1

I have some JS code (Swarmify) running on every page of my website (mydomain.com) that changes the skin of YouTube videos (to Swarmify).

I can modify this code in the parent site dashboard.

I want to exclude it from running on certain pages (mydomain.com/blog*), by editing the parent code; i.e. I want all my blog videos to be simply embedded YouTube videos without needing to edit each individual blog post.

I don't know JS at all.

Is there a way/command I can insert that will add the URL string exception? (and using a wildcard; i.e. all pages that begin mydomain.com/blog*)

Here is the code (the cdnkey has been amended : )).

<!-- start Swarmify JS code-->
<script data-cfasync="false">
    var swarmoptions = {
        swarmcdnkey: "myrandomstring",
        iframeReplacement: "iframe",
        autoreplace: {
            youtube: true
        },
        theme: {
            primaryColor: "#eb6a56"
        }
    };
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
<!-- end Swarmify JS code-->

I contacted the developer but they were unable to help.

As per this post answer, I added the suggested JS script (see below) which stopped Swarmify running (good!), but stopped it on all pages of my website, not just blog pages (bad!).

Perhaps I didn't add that code in the right way?

<script data-cfasync="false">
if (window.location.href.indexOf('/blog*') != -1) {
   //don't run on blog pages
   return;
}
    var swarmoptions = {
        swarmcdnkey: "myrandomstring",
        iframeReplacement: "iframe",
        autoreplace: {
            youtube: true
        },
        theme: {
            primaryColor: "#eb6a56"
        }
    };
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
RoCrow
  • 31
  • 5
  • Check the URL before executing the code? You've got the right idea but it should be "if url is this, then run that", not "if url is not this, return", because you can't return at the top level. – kelsny Mar 25 '22 at 15:36
  • You can't do `String.indexOf('blog*')` It will always return false unless there is a literal character `*` in the string. If you want to use a wildcard, use a regex like `/\/blog.*/`. – 2pichar Mar 25 '22 at 15:54
  • And also, you can't use a return statement in a top-level statement. Only in functions. – 2pichar Mar 25 '22 at 15:55

1 Answers1

0

You can try the below code. I hope it will solve your problem.

<script data-cfasync="false">
        var swarmoptions = {};
        window.addEventListener('DOMContentLoaded', (event) => {
        if (/\/blog[.]*/ig.test(window.location.toString())) {

            return;
        }
        swarmoptions = {
            swarmcdnkey: "myrandomstring",
            iframeReplacement: "iframe",
            autoreplace: {
                youtube: true
            },
            theme: {
                primaryColor: "#eb6a56"
            }
        };
    });
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
Jaied
  • 900
  • 9
  • 13