Facebook的Chrome扩展程序仅在刷新时加载 [英] Chrome extension for Facebook loads just on refresh

查看:124
本文介绍了Facebook的Chrome扩展程序仅在刷新时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个扩展,它应该会显示'Hi。'每隔时间用户点击一个页面,这就是代码:

manifest.json

  {
name:first-ext,
版本:1.0,
manifest_version:2,

权限:[
标签
],
content_scripts:
[
{
matches:[https://www.facebook.com/*],
js:[jquery .js,f.js],
css:[style.css]
}
]

}



f.js

  $(document).ready(function(){
alert(Hi。);
});

我正在使用这些文件,如果我刷新每个页面,一切正常但 strong>如果我通过点击每个链接来更改页面,那么脚本不会在新页面上运行。

例如,如果打开Facebook主页的扩展名是Hi。
如果现在我刷新它再次工作。
如果我点击朋友的时间线链接,浏览器将打开页面,但扩展名不起作用。就像脚本没有执行一样。
如果我刷新,那么它再次工作。



这是不寻常的,因为我在其他网站上尝试相同的扩展,并且如果我刷新并且如果我更改页面通过点击一些链接。



对不起,我的英语不错。

解决方案

您应该使用 MutationObserver 。它将允许您对DOM中的更改做出反应。当与 TreeWalker 一起使用时,它特别有用。



请参阅此示例扩展程序详细了解如何使用它。


I wrote an extension which should say 'Hi.' every time the user clicks on a page and this is the code:

manifest.json

{
"name" : "first-ext",
"version" : "1.0",
"manifest_version" : 2,

"permissions":[
    "tabs"
],
"content_scripts":
[
    {
        "matches" : ["https://www.facebook.com/*"],
        "js": ["jquery.js", "f.js"],
        "css": ["style.css"]
    }
]

}

f.js

$(document).ready(function(){ 
    alert("Hi."); 
});

I'm using those files and if I refresh every page everything works fine but if I change page by clicking on every link then the script do not run on the new page.

For example if open the Facebook home page the extension say "Hi."; If now I refresh it works again. If I click on a link of a time line of a friend then the browsers opens the page but the extension don't work. It's like if the script is not executed. If I refresh then it works again.

It's unusual because I tried the same extension on other sites and it works fine if I refresh and if I change page by clicking some link.

Sorry for my english.

解决方案

You should use MutationObserver. It will allow you to react to changes in the DOM. It is especialy powerfull when used together with TreeWalker.

See this sample extension for details on how to use it.

这篇关于Facebook的Chrome扩展程序仅在刷新时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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