Chome扩展:如果引荐来源网址是Google,则显示网页操作

时间:2016-06-15 07:06:45

标签: javascript google-chrome-extension

我希望Chrome扩展程序页面操作显示引荐来源网址是否为Google,但我的代码无效。

的manifest.json

 "description": "Shows a page action if referrer url is google.",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "page_action" :
  {
    "default_icon" : "icon-19.png",
    "default_title" : "The referrer is Google!"
  },
  "permissions" : [
    "declarativeContent",
    "tabs"
  ],
  "icons" : {
    "48" : "icon-48.png",
    "128" : "icon-128.png"
  },
  "manifest_version": 2
}

background.js

chrome.runtime.onInstalled.addListener(function() {

  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {

    chrome.declarativeContent.onPageChanged.addRules([
      {
          if ('document.referrer' == "https://www.google.*") {
              new chrome.declarativeContent.ShowPageAction()
          }

      }
    ]);
  });
});

我希望页面操作显示引荐来源网址是否为google。请帮忙。

1 个答案:

答案 0 :(得分:2)

'document.referrer' == "https://www.google.*"是一个无操作,因为你只是比较两个不同的字符串,它们永远不会相互相等。

同时,Declarative Content API允许您根据URL和CSS选择器显示pageAction,而不是意味着您可以访问网页中的document对象。

您需要使用chrome.pageAction.show来获取推荐人,您可以致电

chrome.tabs.executeScript(TAB_ID, {code: 'document.referrer;'}, (result) => console.log(referrer));
相关问题