I am trying to create an Firefox add-on/extension to add a parameter to the end a URL of a particular page and reload the page with the new URL with the parameter added.
The problem with the following code is that it does add the parameter, but it keeps reloading the new page uncontrollably
initial URL https://www.example.com/questions/foo/bar
paged reloaded with changed URL https://www.example.com/foo/bar?abcd=1
script.js
var url = window.location.href;
if (url.indexOf('?') > -1){
window.stop()
}else{
url += '?abcd=1'
}
window.location.href = url;
manifest.json
{
"manifest_version": 2,
"name": "some name",
"version": "1.0",
"description": "some description",
"icons": {
"48": "icons/explore-48.png"
},
"content_scripts": [
{
"matches": ["*://www.example.com/*"],
"js": ["script.js"]
}
]
}
Note : I did try out few examples found for the same scenario from Stackoverflow
JavaScript doesn't wait for the whole if
statement to execute before continuing to what is after it, so the value of url
never changes before the page is reloaded. To fix this, simply move window.location.href = url;
into your else
, like this:
var url = window.location.href;
if (url.indexOf('?') > -1) {
window.stop()
} else {
url += '?abcd=1'
window.location.href = url;
}