如何在关注链接后再次运行内容脚本

时间:2016-09-19 20:53:30

标签: javascript google-chrome-extension content-script

我在加载edmodo.com时有一个成功运行的内容脚本,但是当我在帖子中关注链接时,脚本不会修改新内容。我知道我必须遗漏一些明显的东西,但我仍在学习Chrome扩展程序的工作原理。我很感激任何建议。

的manifest.json

{
  "name": "Spell Check Disabler",
  "version": "1.0",
  "description": "Prevents Chrome's spell check in text boxes on Edmodo.com",

  "content_scripts": [
    {
      "matches": ["*://*.edmodo.com/*"],
      "js": ["jquery-3.1.0.min.js","page.js"]
    }
  ],

  "permissions": [
   "activeTab"
   ],

  "manifest_version": 2
}

page.js

$(document).ready(function(){

    console.log("DOM: " + Date());  //just checking to see if it loads

    $("input").focus(function(){
        $(this).attr('spellcheck', false);
    });
    $("input").blur(function(){
        $(this).attr('spellcheck', false);
    });

    document.body.style.background = 'yellow';  //another indicator to see if it loaded
});

document.title = 'spellcheck disabled'; //another indicator to see if it loaded

页面背景变为黄色,单击链接后仍保持黄色,但页面标题更改以反映新页面,我在控制台中没有获得任何新条目。

我已经尝试过使用后台脚本和chrome.webNavigation监听器,但没有成功。

FWIW,页面的网址以 https://www.edmodo.com/home#/ 开头,我点击的链接包含一个链接到 href =“/ home#/ quiz / start /的按钮quiz_run_id / 12078109"

由于 -keen

1 个答案:

答案 0 :(得分:0)

点击从https://www.edmodo.com/home#//home#/quiz/start/quiz_run_id/12078109的链接不会导致导航,因此无法真正建立链接。它只是改变位置哈希。这就是为什么你的脚本不再运行,它仍然是同一个文件。

您的链接最有可能由JavaScript处理,您需要在这些点击事件上再次运行代码(或者在之后的某个时刻,加载其他内容时),或者只是使用event delegation这样你的事件监听器就可以应用于未来的元素。