Search code examples
phphtmlscriptingtumblrifttt

How to check links and change them?


I am working on an Automated Tumblr blog and noticed that you can't post Twitch streams as a video post, however when you embed them you can.

There are going to be multiple users and it will be expanding, so I am trying to automate the script in IFTTT, however, I don't have much scripting knowledge and tried to get this done... didn't work out.

The question:

It all starts with the content link, this can really be any platform. However, all platforms that aren't supported by Tumblr need a different embed.

So what I want is a script (not asking for the script, just help) that can detect what platform the link is from and chooses a path on what it found out.

Basically, a script that checks if it's twitch, run a, if it's youtube run b, etc.

If the input contains "Twitch" run:

<html>
  <body>
    <div id="twitch-embed"></div>
    <script src="https://embed.twitch.tv/embed/v1.js"></script>
    <script type="text/javascript">
      new Twitch.Embed("twitch-embed", {
        width: 854,
        height: 480,
        channel: "{channel Name}"
      });
    </script>
  </body>
</html>

If YouTube:

<script type="text/html" id="Video URL here">

How do I create these checks?

(I know this question is stupid and probably easy, but I am not really big on scripting)


Solution

  • You can take the URL string and check it against each keyword to determine if it exists:

    var urlString = "https://www.twitch.tv/Reco_Mouse";
    
    if(urlString.indexOf("twitch.tv") !== -1) {
    
      //First, dynamically include the Twitch script tag on the page
    
      new Twitch.Embed("twitch-embed", {
        width: 854,
        height: 480,
        channel: "{channel Name}"
      });
      break;
    
    } else if(urlString.indexOf("youtube.com") !== -1) {
    
      //Apply necessary steps for YouTube videos
    
    } else if(urlString.indexOf("anothervideotype.com") !== -1) {
    
      ... //Apply necessary steps for another video type
    
    }