50

「HelloWorld」というJavaScriptアラートを表示する単純なChrome拡張機能を作成しています。この例では、拡張機能がgoogle.comに対してのみ実行されるように指定しました(これをmanifest.json内のpermissionsプロパティに配置することにより)。

ターゲットページのすべてがロードされた後でも、アラートは表示されません。これまでの私のスクリプトは次のとおりです。

ファイル:manifest.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が読み込むすべてのページに挿入できます。matchesマニフェストファイルのおよびexclude_matchesサブアイテムを使用して、挿入されたスクリプトを取得するページを指定できます。

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

必ず名前をに変更Hello.htmlしてくださいhello.js(そして<script>タグを削除してください)。

また、 HTTPおよびHTTPSを介してGoogleに適用されるようにに変更したことにも注意してくださいhttp://*.google.com/(また、末尾には、メインページだけでなく上のすべてのページに適用されることが保証されます)。*://*.google.com/**google.com

于 2012-05-03T06:25:51.423 に答える
6

特定のページでのみアイコンを有効にする方法を見つけようとして、この答えに出くわしました。これが私が行った方法です。ドキュメント

background.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 に答える