I've got some jQuery code that does some specific stuff on certain webpages, and doesn't load on others. Here is my current method of running said code:
if ((window.location.href).indexOf('somewebsite.com') >= 0){
chrome.extension.sendMessage({greeting: "loadscript"});
var stuff = new Stuff();
//run some code
dothiseverytime(withSomeParams);
} else if ((window.location.href).indexOf('someotherwebsite.com') >= 0){
chrome.extension.sendMessage({greeting: "loadscript"});
var stuff = new Stuff();
//run some code
dothiseverytime(withDifferentParams);
} else if
// etc..
I'm wondering if I could do something along the lines of a switch case using indexOf and an array. Maybe something along the lines of this pseudocode?
someWebsites = ['somewebsite.com','someotherwebsite.com']
function checkTabURL {
switch ((window.location.href).indexOf(someWebsites) >= 0)
case 0 // first site in our list - index 0
var stuff = new Stuff();
// do some stuff
case 1 // second site on our list - index 1
var stuff = new Stuff();
// do some other stuff
case -1 // site isn't on the list
// don't do anything
}
I'd like to minimize my code and I think using something along those lines would reduce the amount of code written as well.
Since people are confusing what I need and providing the opposite (searching the URL against an array instead of an array against the URL) - I'd like to clarify.
My array may contain things like 'somesite.com/subdir' so I cannot match the URL to the array - I need to match the array to the URL. I need to see if ANYTHING in the array is in the current URL (and then execute a case), not the other way around.
IE: Is 'somesite.com/subdir' contained in the current URL? Is 'someothersite.com' in the current URL? Execute case 0 for the former, case 1 for the latter. Case -1 if neither.