Chrome Extension run for a specific page

前端 未结 1 1687
时光取名叫无心
时光取名叫无心 2020-12-13 09:07

I\'m writing a simple Chrome extension that displays a JavaScript alert saying \"Hello World\". In this example I have specified that the extension will only run for google.

相关标签:
1条回答
  • 2020-12-13 09:27

    You are adding a browser action popup, which adds a button to the top-right of your browser. (It's probably invisible because you haven't specified an image for it. There should be some empty space to the right of your address bar; try clicking it to see your Hello.html in a popup.)

    What you want is a content script. Content scripts can get injected into every page that Chrome loads. You can use the matches and exclude_matches sub-items in your manifest file to specify which pages get your injected script.

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

    Make sure you rename Hello.html to hello.js (and get rid of the <script> tags).

    Note also that I changed your http://*.google.com/ to *://*.google.com/* so that it will apply to Google over HTTP and HTTPS (and the trailing * ensures that it will apply to all pages on google.com, not just the main page).

    0 讨论(0)
提交回复
热议问题