对阿贾克斯的改变浏览器扩展程序运行的内容脚本脚本、对阿、浏览器、程序

2023-09-10 14:07:50 作者:帅哥哥的热头脑

我有一个Chrome扩展,这使得在网站上(编辑评论)的一些变化。

在网站上最近的变化(网站是不是我的)之后 - 用ajax注释块负载(之前它与整个页面重新加载简单的POST请求)

现在,如果我加载页面第一次 - 内容脚本的工作,但是当我去到下一个页面,说网页#2 - 注释使用Ajax和扩展脚本不再运行增加。所以注释不会改变我想要的方式。

有没有简单的方法来听页面更改DOM和再次申请延期脚本?

在清单文件我有:

  {
  名:姓名,
  版本:1.0,
  说明:说明,
  图标:{16:16.png
            48:48.png
            32:32.png,
            128:128.png},
  browser_action:{
    default_title:默认标题
    default_icon:48.png,
    弹出:popup.html
  },
  权限:
    标签,
    http://www.site.com/*
    通知
    unlimitedStorage
  ]

  options_page:options.html,
  background_page:background.html,

  content_scripts:[
                {
                    比赛:http://www.site.com/*","https://www.site.com/*],
                    JS:jQuery的-1.7.1.min.js,content.js],
                    run_at:document_end
                }]
}
 
网络遨游哪家强 主流PC浏览器对比体验

解决方案

您可以在名为 DOMSubtreeModified 事件添加一个侦听器。功能将其绑定,它被调用只要有一个变化。

在jQuery的:

  $('#ContentContainer')绑定(DOMSubtreeModified',modifyComments);
 

更新:

如果该事件被射击多次,删除活动期间第一次结合,并有一定超时后,你可以调用modifyComments并重新绑定的事件。

 函数DOMModificationHandler(){
    $(本).unbind('DOMSubtreeModified.event1');
    的setTimeout(函数(){
        modifyComments();
        $('#ContentContainer')绑定(DOMSubtreeModified.event1',DOMModificationHandler);
    },1000);
}

//文件加载后,
$('#ContentContainer')绑定(DOMSubtreeModified.event1',DOMModificationHandler);
 

I have a chrome extension which makes some changes on the site (editing comments).

After recent changes on the site (site is not mine) - the comment block loads using ajax (before it was simple post request with the whole page reload).

Now if I load the page first time - content script works, but when I go to next page, say page #2 - the comments are added using ajax and the extension script is not run anymore. So the comments are not changed the way I want.

Is there any simple way to listen to page changes DOM and apply extension script again?

in manifest file I have:

{
  "name": "Name",
  "version": "1.0",
  "description": "Description",
  "icons": {"16":"16.png",
            "48":"48.png",
            "32":"32.png",
            "128":"128.png"},
  "browser_action": {
    "default_title": "Default title",
    "default_icon": "48.png",
    "popup": "popup.html"
  },
  "permissions": [
    "tabs",
    "http://www.site.com/*",
    "notifications",
    "unlimitedStorage"
  ],

  "options_page": "options.html",
  "background_page": "background.html",

  "content_scripts": [
                {
                    "matches": ["http://www.site.com/*","https://www.site.com/*"],
                    "js": ["jquery-1.7.1.min.js","content.js"],
                    "run_at": "document_end"
                }]
}

解决方案

You could add a listener on the event called DOMSubtreeModified. Bind a function to it, and it gets called whenever there's a change.

In jQuery:

$('#ContentContainer').bind('DOMSubtreeModified',modifyComments);

UPDATE:

In case the event is firing multiple times, delete the event binding during the first time, and after a certain timeout, you can call modifyComments and rebind the event.

function DOMModificationHandler(){
    $(this).unbind('DOMSubtreeModified.event1');
    setTimeout(function(){
        modifyComments();
        $('#ContentContainer').bind('DOMSubtreeModified.event1',DOMModificationHandler);
    },1000);
}

//after document-load
$('#ContentContainer').bind('DOMSubtreeModified.event1',DOMModificationHandler);