簡體   English   中英

內容腳本只運行一次(臨時插件)

[英]Content script only runs once (temporary addon)

我做了一個簡單的臨時插件:

manifest.json

{

  "manifest_version": 2,
  "name": "Content script test",
  "version": "1.0",

  "description": "TEST",

  "content_scripts": [
    {
      "matches": ["*://*.telex.hu/*"],
      "js": ["content-script.js"]
    }
  ]

}

內容腳本.js

console.log("*** Content script is running for " + document.location.href);

window.addEventListener(
        'load',
        function(){ console.log("*** Hello World!"); },
        true
    );

我在 FireFox 和 Chrome 上對其進行了測試。

當我打開一個新的瀏覽器選項卡並加載頁面時,例如https://telex.hu ,然后我的插件運行正常(我在開發人員控制台中看到了日志)。 如果我右鍵單擊頁面上的鏈接,則所選頁面(同一域)會在新選項卡中打開,並且插件可以正常運行。 但是,如果我左鍵單擊一個鏈接並在同一選項卡中打開所選頁面(同一域),那么什么也不會發生 - 插件不會運行(控制台上沒有顯示日志)...

解釋是什么? 我應該怎么做才能使插件在每種情況下都運行???

解決方案是:

內容腳本.js

let lastUrl = location.href; 
new MutationObserver(() => {
  const url = location.href;
  if (url !== lastUrl) {
    lastUrl = url;
    onUrlChange();
  }
}).observe(document, {subtree: true, childList: true});

function onUrlChange() {
  console.log('*** URL changed!', location.href);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM