简体   繁体   English

我的firefox扩展程序的javascript代码使页面无法重新加载

[英]javascript code of my firefox extension keeps reloading the page uncontrolablly

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. 我正在尝试创建Firefox附加组件/扩展名,以在特定页面的URL末尾添加一个参数,并使用添加了参数的新URL重新加载页面。

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 初始网址 https://www.example.com/questions/foo/bar

paged reloaded with changed URL https://www.example.com/foo/bar?abcd=1 分页后重新加载了更改的URL https://www.example.com/foo/bar?abcd=1

script.js 的script.js

var url = window.location.href;    
if (url.indexOf('?') > -1){
   window.stop()
}else{
   url += '?abcd=1'
}
window.location.href = url; 

manifest.json 的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 注意 :我确实尝试了一些从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. JavaScript不会等待整个if语句执行之后再继续执行后续操作,因此url的值在重新加载页面之前永远不会改变。 To fix this, simply move window.location.href = url; 要解决此问题,只需移动window.location.href = url; into your else , like this: 进入您的else ,就像这样:

var url = window.location.href;
    if (url.indexOf('?') > -1) {
        window.stop()
    } else {
        url += '?abcd=1'
        window.location.href = url;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM