Chrome扩展:Facebook上的内容脚本 [英] Chrome Extension : content script on facebook
问题描述
我需要在每个页面上执行相同的内容脚本。它在第一次加载期间工作良好,但是当我去一个Facebook页面感谢一个链接(作为配置文件名称)时,它不起作用。
我读这是因为除了第一次之外,不是整个页面被加载。所以脚本不再被执行。
但是我不知道如何解决这个问题。
这是我的manifest.json:
{
/ pre>
name:name,
version:1.0,
manifest_version:1,
description:description,
content_scripts:[
{
matches:[http://www.facebook.com/*],
js:[test.js]
}
],
all_frames:true
}
我希望有人会有答案,因为我真的需要脚本(test.js,不会更改html或css)要执行的脚本每页每页!
感谢您将来的回答。
解决方案我个人使用Chrome的webRequest API解决了这个问题。您将需要添加一个跟踪AJAX生成的HTTP调用的侦听器。这里基本上是我使用的代码:
chrome.webRequest.onCompleted.addListener(function(details){
var url = document.createElement('a');
url.href = details.url;
if(url.search&& url.search.indexOf('ajaxpipe = 1')!== -1){
console.log('通过AJAX的新页面');
chrome.tabs.executeScript({'file':'test.js'});
}
},{urls:[*://*.facebook.com/*]});
I am creating a chrome extension for facebook. I need to execute the same content script on every page. It works well during the first load but it doesn't work when I go to a facebook page thanks to a link (as the profile name). I read this was because not the entire page was loaded except the first time. So the script is not executed again. However I have no idea how to solve this problem.
Here is my manifest.json :
{ "name" : "name", "version" : "1.0", "manifest_version":1, "description" : " description ", "content_scripts" : [ { "matches" : ["http://www.facebook.com/*"], "js" : ["test.js"] } ] , "all_frames":"true" }
I hope someone will have the answer because I really need the script (test.js, which doesn't change the html or the css) to be executed on every page every time!
Thanks for your future answers.
解决方案I personally solved this by using Chrome's webRequest API. You'll want to add a listener that tracks AJAX-generated HTTP calls. Here's essentially the code I used:
chrome.webRequest.onCompleted.addListener(function(details) { var url = document.createElement('a'); url.href = details.url; if (url.search && url.search.indexOf('ajaxpipe=1') !== -1) { console.log('New page via AJAX.'); chrome.tabs.executeScript({'file' : 'test.js'}); } }, {urls : ["*://*.facebook.com/*"]});
这篇关于Chrome扩展:Facebook上的内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!