Chrome扩展程序 - 如何在chrome.browserAction.onClicked发布时执行操作? [英] Chrome Extensions - How can i perform an action, when chrome.browserAction.onClicked has fired?

查看:295
本文介绍了Chrome扩展程序 - 如何在chrome.browserAction.onClicked发布时执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个清单文件和一个后台脚本,脚本就是这个脚本以下内容:

  chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(null ,{code:'some code here'});
});

代码在页面上工作,我用不同的方式尝试了它(弹出窗口和一个按钮,那个行动)。但是,如果我用browserAction onclick方法尝试它,没有任何反应:($ / b>

清单:

  {
name:somename,
version:1.0,
manifest_version:2,
description: sometext,
browser_action:{
default_icon:images / icon.png,
default_title:MyStyle
},
背景:{
scripts:[js / code.js]
},
permissions:[
tabs,
https://www.examplesite.ex/*,
http://www.examplesite.ex/*,
http://*.ex/*
]
}

任何人都可以帮助我吗?:/


由于原来的问题已经在评论中得到了解决,我会回答后续问题:
$ b

$ b 下一步使其自动完成,无需点击


这可以轻松完成使用内容脚本。当你不必访问全局变量时,下面的代码就足够了。否则,请使用以下技术注入脚本:



js / code.js



  document.title =newtitle; 





 <$ c 
name:somename,
version:1.0,
manifest_version:2,
description:sometext,
content_scripts:{
js:[js / code.js],
matches:[*://www.examplesite.ex/*, http://*.ex/*]
},
permissions:[*://www.examplesite.ex/*,http://*.ex/ *]
}


I want to make a browserAction extension, with an icon and a listener on it.

I have a manifest file, and a background script, the script is the following:

chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null,{code:'some code here'});
});

The code works on the page, i tried it on a different way (popup and a button what fires the action). But if i try it with a browserAction onclick method, nothing happens:(

The manifest:

{
  "name": "somename",
  "version": "1.0",
  "manifest_version": 2,
  "description": "sometext",
  "browser_action": {
    "default_icon": "images/icon.png",
    "default_title": "MyStyle"
  },
  "background": {
    "scripts": ["js/code.js"]
  },
  "permissions": [
    "tabs",  
    "https://www.examplesite.ex/*",
    "http://www.examplesite.ex/*",
    "http://*.ex/*"
  ]
}

Can anybody help me?:/

解决方案

Since the original question has been solved in the comments, I'll answer the follow-up question:
"Next step to make it automatic, without any click".

This can be done easily using Content scripts. When you don't have to access global variables, the following code is sufficient. Otherwise, inject the script using the techniques as mentioned here:

js/code.js

document.title = "newtitle";

manifest.json

{
  "name": "somename",
  "version": "1.0",
  "manifest_version": 2,
  "description": "sometext",
  "content_scripts": {
    "js": ["js/code.js"],
    "matches": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
  },
  "permissions": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
}

这篇关于Chrome扩展程序 - 如何在chrome.browserAction.onClicked发布时执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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