简体   繁体   English

Chrome扩展程序:如何获取内容脚本扩展程序以显示在某些网站上

[英]Chrome Extension: How do I get me Content Script Extension to show up on select websites

I would like my Chrome Extension to on show up on google and amazon. 我希望我的Chrome扩展程序可以在Google和亚马逊上显示。 My manifest.json looks like this: 我的manifest.json看起来像这样:

{
 "background": {"scripts": ["background.js"]},
 "content_scripts": [
    {
      "matches": ["*://*.google.com/*", "http://www.amazon.com/*", "*://*.amazon.com/*"],
      "js": ["background.js"]
    }
  ],
 "name": "Denver Public Library Lookup",
 "description": "Does Stuff",
 "homepage_url": "http://www.artifacting.com",
 "icons": {
     "16": "icon-16.png",
     "48": "icon-48.png",
     "128": "icon-128.png" },
 "permissions": [
     "tabs",
     "http://*/*",
     "https://*/*"
 ],
 "version": "1.0",
 "manifest_version": 2
}

But it doesn't show up on either google or amazon and I can't figure out why. 但是它没有出现在谷歌或亚马逊上,我也不知道为什么。

Here is my background.js 这是我的background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "bookmarklet.js"})
});

And here is the bookmarlet.js 这是bookmarlet.js

setTimeout('x99.focus()', 300);
var re = /([\/-]|at[at]n=)/i;
if (re.test(location.href) == true) {
    var isbn = RegExp.$2;
    var x99 = window.open('http://searchsite/search/searchresults.aspx?ctx=1.1033.0.0.6&type=Keyword&term=' + atatn, 'Library', 'scrollbars=1,resizable=1,top=0,left=0,location=1,width=800,height=600');
    x99.focus();
}

Any ideas? 有任何想法吗? Thanks for your help. 谢谢你的帮助。

Many mistakes in the code. 代码中有很多错误。

  • You don't need content script here, all operations could be executed within background page content 您这里不需要内容脚本,所有操作都可以在后台页面内容中执行
  • It it hard to make background page code works within content script, this is definitely not a your case. 很难使后台页面代码在内容脚本中工作,这绝对不是您的情况。 So using the same background.js as both background and content script does not work in your case at least 因此,至少在您的情况下,无法同时使用与背景脚本和内容脚本相同的background.js
  • Manifest does not declare browser action. 清单不声明浏览器动作。
  • And so on 等等

I strongly suggest to start with Google extension documentation . 我强烈建议从Google扩展程序文档开始。 You will save a lot of your time. 您将节省大量时间。

How I think files might look like 我认为文件可能看起来像

manifest.json 的manifest.json

{
 "background": {"scripts": ["background.js"]},
 "name": "Denver Public Library Lookup",
 "description": "Does Stuff",
 "homepage_url": "http://www.artifacting.com",
 "icons": {
     "16": "icon-16.png",
     "48": "icon-48.png",
     "128": "icon-128.png" },
  "browser_action": {
    "default_icon": {
      "19": "images/icon-19.png",
      "38": "images/icon-38.png"
    },
    "default_title": "Do Staff"      // optional; shown in tooltip
  },
 "permissions": [
     "tabs",
     "http://*/*",
     "https://*/*"
 ],
 "version": "1.0",
 "manifest_version": 2
}

background.js background.js

chrome.browserAction.onClicked.addListener(function(tab) {
  // You need more sothisticated regexp here which checks for amazon and google domains
  var re = /([\/-]|at[at]n=)/i;
  if (re.test(tab.url)) {
    var isbn = RegExp.$2;
    var url = "http://searchsite/search/searchresults.aspx?ctx=1.1033.0.0.6&type=Keyword&term=" + isbn;
    chrome.windows.create({
      url : url, 
      left: 0,
      top: 0,
      width: 800,
      height: 600,
      focused: true,
      type: "popup"
    });
  }
});

bookmarlet.js is not needed 不需要bookmarlet.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在后台获取chrome扩展程序的内容脚本? - How can I get chrome extension's content script to in the background? Chrome 扩展程序:如何获取 web 页面以访问我的内容脚本定义的功能? - Chrome Extension: How do I get a web page to access functions defined by my content script? Chrome扩展程序的内容脚本未注入特定网站 - Chrome extension's content script not getting injected on specific websites 如何从 chrome 扩展中的内容脚本中获取另一个文件内容 - How to get another file content from content script in chrome extension Chrome 扩展开发者 - 如何匹配来自用户指定网站的内容脚本 url? - Chrome Extension Developer - how to match content script urls from user specified websites? 如何将获取的数据从 chrome 扩展后台脚本发送到内容脚本? - How do I send fetched data from chrome extension background script to content script? 如何将内容脚本变量发送到后台脚本? (Chrome 扩展程序) - How do I send a content script variable to a background script? (Chrome Extension) 如何将 chrome 扩展中的选定文本从后台脚本传递到内容脚本? - how do i pass selected text in chrome extension from background script to content script? 如何在Chrome扩展程序内容脚本中获取D3节点的数据 - How to get the data of a D3 node in Chrome Extension content script 如何在Chrome扩展程序内容脚本中获取错误堆栈跟踪? - How to get errors stack trace in Chrome extension content script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM