仅在chrome扩展名上单击运行脚本

[英]Run the script only onclick on chrome extension


Whenever my chrome extension icon is clicked, I want to run a script that would make certain changes to the current webpage.

每当我点击Chrome扩展程序图标时,我想运行一个脚本,对当前网页进行某些更改。

I have tried using content_scripts in my manifest and it worked but the problem is , the script runs even if I did not clicked on the icon.

我已经尝试在我的清单中使用content_scripts但它有效但问题是,即使我没有点击图标,脚本也会运行。

I have found that, I need to use background script.In my background.js file I have added

我发现,我需要使用后台脚本。在我的background.js文件中我添加了

chrome.browserAction.onClicked.addListener(function(tab) {
  alert();
});

and it is not working.

它不起作用。

Here is my manifest file.

这是我的清单文件。

    {
  "manifest_version": 2,

  "name": "Reveal Password",
  "description": "Reveals password in password input field",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": {
    "scripts": ["background.js"]
  }
}

Plus I want to execute the script that I made that manipulates the current web page too.

另外,我想执行我操作当前网页的脚本。

1 个解决方案

#1


0  

Use chrome.tabs.executeScript() to execute code in a tab like this:

使用chrome.tabs.executeScript()在选项卡中执行代码,如下所示:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript({
    code: 'alert("Hello")'
  });
});

Instead of code, you could also use a file which contains the code :

您也可以使用包含代码的文件而不是代码:

chrome.tabs.executeScript(null, {file: "content_script.js"});

NOTE : You need activeTab permissions to execute code in an active tab

注意:您需要activeTab权限才能在活动选项卡中执行代码

"permissions": [
  "activeTab"
]
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/11/27/4ca94ad9622056afb70aac66d5680e0c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告