我的firefox扩展的javascript代码一直在不受控制地重新加载页面
我正在尝试创建一个Firefox插件/扩展,在特定页面的URL末尾添加一个参数,并使用添加了参数的新URL重新加载页面 下面代码的问题是,它确实添加了参数,但仍会不可控地重新加载新页面 初始URL 使用更改的URL重新加载页面 script.js我的firefox扩展的javascript代码一直在不受控制地重新加载页面,javascript,firefox-addon,firefox-addon-webextensions,page-refresh,Javascript,Firefox Addon,Firefox Addon Webextensions,Page Refresh,我正在尝试创建一个Firefox插件/扩展,在特定页面的URL末尾添加一个参数,并使用添加了参数的新URL重新加载页面 下面代码的问题是,它确实添加了参数,但仍会不可控地重新加载新页面 初始URL 使用更改的URL重新加载页面 script.js var url = window.location.href; if (url.indexOf('?') > -1){ window.stop() }else{ url += '?abcd=1' } window.loca
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"]
}
]
}
注意:我尝试了Stackoverflow中为同一场景找到的几个示例JavaScript不会等到整个
if
语句执行后再继续执行,因此url
的值在页面重新加载之前不会改变。要解决此问题,只需移动window.location.href=url编码>到您的else
,如下所示:
var url = window.location.href;
if (url.indexOf('?') > -1) {
window.stop()
} else {
url += '?abcd=1'
window.location.href = url;
}