相关疑难解决方法(0)

使用函数触发chrome.browserAction.onClicked

我想触发以下代码正在侦听的点击:

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

原因是我有一个工作扩展,它在后台脚本(上面的addListener)中监听并在点击时执行一些脚本:

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: 'abc.js'});
chrome.tabs.executeScript(tab.id, {file: 'xxx.js'});
});
Run Code Online (Sandbox Code Playgroud)

现在我想从上下文菜单中触发这个"onClicked":

var myContextPage = chrome.contextMenus.create({"title": myTitle, "contexts":["page"],
                                     "onclick": fctContext});
Run Code Online (Sandbox Code Playgroud)

所以,我想,最简单的方法是在fctContext中"点击".也许有更好的方法,但我不知道如何解决我的问题.我也尝试过运行"executeScript",但这两种方法都不行.

提前致谢!

//更新

答案解答: 此解决方案正在运行:

//Shared code. When the argument length is two, it is coming from the context
// menu, while a single argument is coming from the browser action.
function fctContext() {
   var tab = arguments.length == 2 ? arguments[1] : arguments[0];
   // Do whatever you want with the tab.
}

// Browser Action
chrome.browserAction.onClicked.addListener(fctContext);

// …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-extension

8
推荐指数
1
解决办法
6156
查看次数