50

我正在编写一个简单的 Chrome 扩展程序,它显示一个 JavaScript 警报,上面写着“Hello World”。在此示例中,我已指定扩展仅针对 google.com 运行(通过将其放在 manifest.json 中的权限属性中)。

即使目标页面中的所有内容都已加载,警报也不会出现。到目前为止,这是我的脚本:

文件:清单.json

{
  "name": "Hello",
  "version": "1.0",
  "description": "Says hello to Google",
  "permissions": ["http://*.google.com/"]
  "browser_action": {
    "popup": "Hello.html"
  }
}

文件:Hello.html

<script language="Javascript">
   alert("Hello World");
</script>
4

3 回答 3

77

您正在添加一个浏览器操作弹出窗口,它会在浏览器的右上角添加一个按钮。(它可能是不可见的,因为您尚未为其指定图像。地址栏右侧应该有一些空白区域;尝试单击它以Hello.html在弹出窗口中查看您的。)

你想要的是一个内容脚本。内容脚本可以注入到 Chrome 加载的每个页面中。您可以使用清单文件中的matchesexclude_matches子项来指定哪些页面获取您的注入脚本。

{
  "name": "Hello",
  "version": "1.0",
  "description": "Says hello to Google",
  "permissions": ["tabs", "*://*.google.com/*"],
  "content_scripts": [
    {
      "matches": ["*://*.google.com/*"],
      "js": ["hello.js"]
    }
  ]
}

确保您重命名Hello.htmlhello.js(并删除<script>标签)。

另请注意,我将您的更改http://*.google.com/为,*://*.google.com/*以便它将通过 HTTP 和 HTTPS 应用于 Google(并且尾随*确保它将应用于 上的所有页面google.com,而不仅仅是主页)。

于 2012-05-03T06:25:51.423 回答
6

我遇到了这个答案,试图找到一种仅在某些页面上启用图标的方法,这就是我的做法。文档

背景.js

chrome.runtime.onInstalled.addListener(function() {
  chrome.tabs.onActivated.addListener(async info => {
    const tab = await chrome.tabs.get(info.tabId);
    
    const isGithub = tab.url.startsWith('https://github.com/');
    isGithub 
      ? chrome.action.enable(tab.tabId) 
      : chrome.action.disable(tab.tabId);
  });
});

确保在清单中添加选项卡权限

于 2021-03-08T04:26:03.240 回答
2
First of all there are 2 types of extensions:

1. Browser Action - which work for multiple websites or almost all websites
2. Page Action - which work for specific websites or webpages [which is needed 
   in our case]

Follow these steps to show your extension only on google:

Step 1: Go to manifest.json file and add the below code snippet

        "background":{
           "scripts":["background.js"],
           "persistent":false
         }
         
         ***also make sure you have page action not browser action** 
        
         "page_action" : { "default_popup":"your_popup.html" }

Step 2: Now add permissions in manifest:
        
        "permissions":["declarativeContent"]

Step 3: Now create background.js in root folder of extension and add the 
        below code in it, this will let the extension to work only on 
        urls that contain google.com
        
        // When the extension is installed or upgraded ...
        chrome.runtime.onInstalled.addListener(function() {
         // Replace all rules ...
         chrome.declarativeContent.onPageChanged.removeRules(undefined, 
      function() {
         // With a new rule ...
         chrome.declarativeContent.onPageChanged.addRules([
         {
          // That fires when a page's URL contains a 'g' ...
          conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlContains: 'google.com' },
          })
         ],
          // And shows the extension's page action.
          actions: [ new chrome.declarativeContent.ShowPageAction() ]
        }
      ]);
    });
  });

Step 4: Now reload your extension, you'll find that your extension will work 
        only for google.com

Hope this solved your query, If Yes, then Upvote the answer Thanks! 

        
       
于 2021-07-21T11:32:11.997 回答