0

I'm making a chrome extension and i want to know the correct match pattern for my URL so that I can execute some javascript

I want it to execute if my url ends in #test

ex. https://blah.com/blah1/blah2/blah3/blah4#test

So it can be any URL as long as it ends in #test.

Any ideas?

user1161310
  • 3,069
  • 3
  • 21
  • 27

1 Answers1

0

You can't do pattern matching for chrome extensions on the hash of a url. See Chrome extension: Content script matching URL pattern for Gmail message. But what you can do is check the document.location.hash property for the term you are looking for and only execute your script there. Then you can have a broader pattern match for the site but only execute the functionality of the site when required.

NB If it's a hard requirement that it has to end with #test then you can simplify things by adding an endsWith function.

Something like:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

if(document.location.hash.endsWith("#test")){ /* do stuff */ }

endsWith taken from endsWith in JavaScript

Community
  • 1
  • 1
Dave Walker
  • 3,498
  • 1
  • 24
  • 25