Chrome扩展:注入代码如果referrer是facebook



我想注入一个代码代码到一个页面,如果推荐人是facebook,但它不工作。这是我在下面使用的代码。

Manifest.json

{
  "name": "Injecta",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Injecting stuff",
  "homepage_url": "http://danharper.me",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": true
  },
  "browser_action": {
    "default_title": "Inject!"
  },
  "permissions": [
    "https://*/*",
    "http://*/*",
    "tabs"
  ]
}

我认为问题在这里。我想注入的代码,如果url引用是facebook。

Background.js

var x = document.referrer;
if (x === "www.facebook.com" ) {
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
	// for the current tab, inject the "inject.js" file & execute it
	chrome.tabs.executeScript(tab.ib, {
		file: 'inject.js'
	});
});
}

Inject.js

// this is the code which will be injected into a given page...
(function() {
	// just place a div at top right
	var div = document.createElement('div');
	div.style.position = 'fixed';
	div.style.top = 0;
	div.style.right = 0;
	div.textContent = 'Injected!';
	document.body.appendChild(div);
	alert('inserted self... giggity');
})();

我将很高兴如果你能提供工作代码给我。

你有两个要求:

  1. 检查推荐人是否是Facebook (FB),
  2. 如果引用者是FB则注入脚本。

第一个需要通过注入的内容脚本在浏览器页面中发生,因为页面的上下文(即referrer)不能直接用于后台脚本。

您可以在所有页面中注入一个简单的引用检测器内容脚本。当检测到referrer=FB时,向后台脚本发送消息,然后后台脚本注入仅限FB的脚本。

或者,您可以在所有页面中注入完整的脚本,但将活动部分(您的inject.js代码)隐藏在referrer === FB的测试后面。

无论哪种方式,对referrer的检测都需要在注入的内容脚本中进行。

如果你想的话,我可以帮你写代码,但我觉得这很简单。正如@wOxxOm所指出的,这是一个架构问题,而不是代码问题。

更新2016-11-08:

重读你的代码,似乎你想要做的是在

  1. Referrer是facebook, and
  2. 用户已按下请求注入的浏览器操作。

已经为这样的场景提供了下面的代码。

  1. 注入一个基本的脚本到所有的页面,通常通过在清单中列出它来完成。
  2. 当浏览器动作被按下时,发送一个消息给始终注入的脚本,询问它标签的引用者是否是facebook。
  3. 脚本响应true,如果referrer是facebook ->现在注入主脚本。

希望对你有帮助。

干杯!

添加代码:

//更新Manifest -添加总是注入的内容脚本

{
  "name": "Injecta",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Injecting stuff",
  "homepage_url": "http://danharper.me",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": true
  },
  "browser_action": {
    "default_title": "Inject!"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["js/inject_all_script.js"]
    }
  ],
  "permissions": [
    "https://*/*",
    "http://*/*",
    "tabs"
  ]
}
//background.js

// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
  // send a message to the 'always-injected' script to ask if the page referrer is facebook
  chrome.tabs.sendMessage(tab.id, {"type": "isFacebook?"}, function(response){
    if(response.answer){
      // if the referrer is facebook, inject the "inject.js" file & execute it
      chrome.tabs.executeScript(tab.ib, {
        file: 'inject.js'
      });
    }
  });
});
//inject_all_script.js

// this is the code which will be injected into *all* pages
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
  if(sender.id === chrome.runtime.id && message.type && message.type === "isFacebook?"){
    sendResponse({"answer": /https?//(www)?.facebook.com/i.test(document.referrer)});
  }
})
//selectively-injected.js

// this is the code which will be injected into pages that have facebook as referrer...
(function() {
  // just place a div at top right
  var div = document.createElement('div');
  div.style.position = 'fixed';
  div.style.top = 0;
  div.style.right = 0;
  div.textContent = 'Injected!';
  document.body.appendChild(div);
  alert('inserted self... giggity');
})();

最新更新