简体   繁体   English

我可以在不属于我的网站上添加事件监听器吗?

[英]Can I add event listener on the site that is not mine?

I would like to know about all changes in some DOM element on one site that is not mine so I could receive browser notifications about DOM element changes. 我想知道一个网站上某些DOM元素的所有更改都不是我的,所以我可以收到有关DOM元素更改的浏览器通知。 How could I do this if I can't write a code on the site? 如果我不能在网站上编写代码,我怎么能这样做? I want to use this approach: 我想用这种方法:

 // select the target node var target = document.getElementById('some-id'); // create an observer instance var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { console.log(mutation.type); }); }); // configuration of the observer: var config = { attributes: true, childList: true, characterData: true }; // pass in the target node, as well as the observer options observer.observe(target, config); // later, you can stop observing observer.disconnect(); 
 <div id="some-id"></div> 

Thank you! 谢谢!

You can always add client side code to any website. 您始终可以将客户端代码添加到任何网站。 That is the main behaviour of browser extensions for example. 例如,这是浏览器扩展的主要行为。

But you can also just take your code and paste it to your browser's console (Chrome: Ctrl + Shift + J or F12 ) 但您也可以将代码粘贴到浏览器控制台(Chrome: Ctrl + Shift + JF12

In this example we will obeserve the <div id="question-header"> of this SO page. 在此示例中,我们将遵守此SO页面的<div id="question-header">

// select the target node
var target = document.getElementById('question-header');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  Notification.requestPermission(function (permission) {
    if (permission === "granted") {
      var notification = new Notification("Something has changed!");
    }
  });
  mutations.forEach(function(mutation) { 
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

Then execute this to test if it is working: 然后执行此操作以测试它是否正常工作:

var node = document.createElement("b");                
var textnode = document.createTextNode("Hello new item");
node.appendChild(textnode);
document.getElementById("question-header").appendChild(node);  

You can achieve that with something like this : 你可以通过以下方式实现这一目标:

$(function(){
    var keep = '';
    var ajaxurl = "http://foo.bar";

    setInterval(function(){
        $.get( ajaxurl, { sample_data : 'test' }, function(response){

            var log = "<p>Checked</p>";

            if( keep != '' && keep !== response )
                log = "<p>Something has changed</p>";

                $('.log-container').append( log );

        });

    }, 2000);
});

but you can do a more advanced thing with pusherJS https://github.com/pusher/pusher-js 但你可以用pusherJS做更高级的事情https://github.com/pusher/pusher-js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM