Chrome扩展程序针对特定页面运行 [英] Chrome Extension run for a specific page

查看:504
本文介绍了Chrome扩展程序针对特定页面运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个chrome扩展,它警告Hello World我为权限指定的页面完成加载,但它不工作,这里是我的脚本

I'm writing a chrome extension just at it alerts a Hello World everything the page i specified for the permission finishes loading but its not working, here is my script

文件:manifest.json

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

文件:Hello.html

File: Hello.html

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


推荐答案

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

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"]
    }
  ]
}

请确保您重命名 Hello。 html hello.js (并去除< script> 标签) 。

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

请注意,我已将 http://*.google.com/ 更改为 *://*.google.com/* ,以便通过HTTP和HTTPS应用于Google(以及后面的 * )可确保它将适用于 google.com 中的所有页面,而不仅仅是主页面)。

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).

这篇关于Chrome扩展程序针对特定页面运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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