触发chrome.browserAction.onClicked一个函数 [英] Trigger chrome.browserAction.onClicked with a function

查看:521
本文介绍了触发chrome.browserAction.onClicked一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

chrome.browserAction.onClicked.addListener(function(tab))

我想触发点击,以下代码正在侦听: {}); $ b

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

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

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

  var myContextPage = chrome.contextMenus.create({title:myTitle,contexts:[page],
onclick:fctContext});

所以,我认为最简单的方法是在fctContext中进行点击。
也许有更好的办法,但我不怎么解决我的问题。

我已经尝试运行executeScript,但这并不奏效。



解答:
此解决方案为: 工作:


  //共享代码。当参数长度为2时,它来自上下文
//菜单,而单个参数来自浏览器操作。
函数fctContext(){
var tab = arguments.length == 2?参数[1]:参数[0];
//根据标签做任何你想做的事。
}

//浏览器操作
chrome.browserAction.onClicked.addListener(fctContext);

//上下文菜单
chrome.contextMenus.create({
title:myTitle,
contexts:['page'],
onclick:fctContext
});

解决方案:测试一些其他内容:

  function fctStart(){
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.executeScript(tab .id,{file:'abc.js'});
chrome.tabs.executeScript(tab.id,{file:'xxx.js'});
});

$ / code>

在这种情况下, fctStart()从任何一点开始工作,而不必通过选项卡。

解决方案

请记住,参数在JavaScript中是可选的。每个函数都有一个关联的参数对象。那个参数就像一个数组。在你的情况下,其中一个需要,而另一个则不需要。在一个函数(浏览器操作)中具有N个参数与在另一个函数(上下文菜单)中具有M个参数相同,这两个参数之间的唯一区别是 arguments.callee 它提供了一种方法来引用函数本身内的实际代码。你不必担心,如果你想要一些基本的东西。



您的 fctContext 您的browserAction点击和您的上下文菜单之间的代码。我在重新加载所有标签扩展中做了类似的操作。



this.reload https://github.com/mohamedmansour/reload-all-tabs-extension/blob/master/js/reload_controller.jsrel =nofollow> https://github.com/mohamedmansour/reload-all-tabs-扩展/ blob / master / js / reload_controller.js ,您会注意到 this.reload 正用于上下文菜单和浏览器操作。您只需分享代码。



通过以下参数示例进行更新:

您的情况完全一样。

  //共享代码。当参数长度为2时,它来自上下文
//菜单,而单个参数来自浏览器操作。
函数fctContext(){
var tab = arguments.length == 2?参数[1]:参数[0];
//根据标签做任何你想做的事。
}

//浏览器操作
chrome.browserAction.onClicked.addListener(fctContext);

//上下文菜单
chrome.contextMenus.create({
title:myTitle,
contexts:['page'],
onclick:fctContext
});

上述方法的问题在于可维护性,如果API更改它可能会中断。就个人而言,我宁愿明确指出参数。因此,用户不必在参数数组中进行查找。

pre $ f $ c $ function fctContext(tab){
//根据标签做任何你想要的。
}

//浏览器操作
chrome.browserAction.onClicked.addListener(fctContext);

//上下文菜单
chrome.contextMenus.create({
title:myTitle,
contexts:['page'],
onclick:function (detail,tab){fctContext(tab)}
});

希望有帮助!


I want to trigger the click, to which following code is listening:

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

The reason is that I have a working extension, which is listening in a background-script (the addListener above) and executes some scripts on click:

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

And now I want to trigger this "onClicked" from a context-menu:

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

So, I thought, that the easiest way would be to have "click" in fctContext. Maybe there is a better way, but I don't how to solve my problem. I also tried to run "executeScript" but this isn't working neither.

Thanks in advance!

//Update

Solution from answers: This solution is working:

//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);

// Context Menu
chrome.contextMenus.create({
   title: myTitle,
   contexts: ['page'],
   onclick: fctContext
});

Solution after testing some other things:

function fctStart() {
  chrome.tabs.getSelected(null, function(tab){ 
    chrome.tabs.executeScript(tab.id, {file: 'abc.js'});
    chrome.tabs.executeScript(tab.id, {file: 'xxx.js'});
  }); 
}

In this case, fctStart() is working from any point without having to pass the tab.

解决方案

Remember that arguments are optional in JavaScript. Each function has an associated arguments object. That arguments acts as an array. In your case, one of them requires that while the other doesn't. Having N arguments is the same in one function (Browser Action) than having M arguments in the other (Context Menu), the only difference between these two arguments is the arguments.callee which provides a way to reference the actual code within the function itself. You don't have to worry about that if you want something basic.

Your fctContext could be the share-able code between your browserAction click and your context menu. I did something similar in the Reload All Tabs extension.

Search for this.reload in https://github.com/mohamedmansour/reload-all-tabs-extension/blob/master/js/reload_controller.js, you will notice that this.reload is being used for the Context Menu and the Browser Action. You just share the code.

UPDATED with the arguments example baked in:

In your case, you do the exact same thing.

// 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);

// Context Menu
chrome.contextMenus.create({
   title: myTitle,
   contexts: ['page'],
   onclick: fctContext
});

The problem with the above approach is maintainability, if the API changes it might break. Personally, I would rather explicitly name the arguments. So the user doesn't have to do a lookup in the arguments array.

function fctContext(tab) {
   // Do whatever you want with the tab.
}

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

// Context Menu
chrome.contextMenus.create({
   title: myTitle,
   contexts: ['page'],
   onclick: function (detail, tab) { fctContext(tab) }
});

Hope that helps!

这篇关于触发chrome.browserAction.onClicked一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆