chrome.tabs.executeScript无效

时间:2017-03-07 00:10:56

标签: javascript google-chrome-extension

我正在制作Chrome扩展程序,以删除暂定主页上的about标签。 executeScript函数不起作用。 清单:

{
  "manifest_version": 2,

  "name": "Scrap",
  "description": "Modifies scratch",
  "version": "1.0",
  "icons": { "19": "icon.png",
            "128": "128icon.png" },
  "browser_action": {
  "default_icon": "icon.png"
  },
  "permissions": [
    "tabs", "<all_urls>", "activeTab"
  ],
  "content_scripts": [
    {
      "matches": ["https://scratch.mit.edu/*"],
      "js": ["background.js"]
    }
  ]
}

background.js

chrome.tabs.executeScript(null, {
    code: 'getElementsByClassName("link about").innerHTML = ""'//this line not working
});

1 个答案:

答案 0 :(得分:1)

我认为您尝试运行的脚本存在问题。

“getElementsByClassName”适用于某个对象,例如“document”。

此外,此方法返回一个元素数组,因此“.innerHTML”将无法工作,因为您需要指定要查找哪些元素的innerHTML。

如果你想要一段有效的代码,只是为了理解我的意思,试试:

document.getElementsByClassName("link about")[0].innerHTML = ""
相关问题