Chrome扩展程序无需点击图标即可加载脚本 [英] Chrome extension to load the script without clicking on icon

查看:254
本文介绍了Chrome扩展程序无需点击图标即可加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我要加载脚本,无论用户是否点击我的扩展图标
这是我的扩展,它工作的很好,但我希望它在没有让用户点击图标来加载脚本的情况下工作。 。

这是代码。

  {
name:Injecta,
version:0.0.1,
manifest_version:2,
description:注入东西,

background:
{
scripts:[jquery.js,background.js]
},
browser_action:{
default_title:注入!

permissions:[
https:// * / *,
http:// * / *,
tabs
]
}

这是我的background.js

  chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript({
file:' jquery.js'
});
chrome.tabs.executeScript({
file:'inject.js'
});
});

我只是希望扩展程序加载所有使用页面加载的脚本。目前用户必须点击该图标才能加载脚本。

解决方案

executeScript 基本上是动态地创建一个内容脚本。这被称为程序化注入



使用内容脚本的另一种方法是在清单中指定它们。这实现了你所要求的内容:内容脚本在页面加载时自动执行。

 content_scripts:[
{
js:[jquery.js,inject.js],
matches:[*:// * / *]
}
],

调整匹配参数只包含匹配模式的页面。



如果您需要微调何时发生注射,请确保查看 run_at 参数的文档。


Hello all i want to load the script whether or not user clicks on my extension icon This is my extension it works great but i want it to work without making the user click on the icon to load the scripts ..

Here is the code .

  {
   "name": "Injecta",
    "version": "0.0.1",
     "manifest_version": 2,
      "description": "Injecting stuff",

        "background":
         {
         "scripts": ["jquery.js","background.js"]
              },
            "browser_action": {
             "default_title": "Inject!"
              },
              "permissions": [
              "https://*/*",
               "http://*/*",
                "tabs"
                  ]
                  }

This is my background.js

     chrome.browserAction.onClicked.addListener(function (tab) {
      chrome.tabs.executeScript({
      file: 'jquery.js'
           });
         chrome.tabs.executeScript({
            file: 'inject.js'
             });
                });

i just want the extension to load all the scripts with the page load. currently user has to click on the icon to load the scripts..

解决方案

What executeScript does is basically creating a Content Script dynamically. This is called Programmatic Injection.

An alternative method of working with content scripts is specifying them in the manifest. This achieves exactly what you're asking: content scripts are executed automatically when the page is loaded.

"content_scripts" : [
   {
     "js": ["jquery.js", "inject.js"],
     "matches": ["*://*/*"]
   }
],

Adjust the matches parameter to only include match patterns for pages you want it to run on.

Make sure to check out the documentation of run_at parameter if you need to fine-tune when injection happens.

这篇关于Chrome扩展程序无需点击图标即可加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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