JS / Chrome扩展程序-EventListener和Mutation无法检测到更改

时间:2019-05-02 08:51:48

标签: javascript google-chrome-extension event-listener

我正在构建一个chrome扩展程序,一旦检测到站点上的更改,就需要调用一个函数。最好是,我想留意<iframe>中发生的更改,但是由于我无法使其正常工作,所以我很乐意在检测到任何更改时就启动代码。

这是我的inject.js文件中的代码:

// run script when site changes are detected
chrome.storage.local.get("obj", function (res) {
    console.log("setting page data object from listener:");
    console.log(res);

    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        findKeyword();
        console.log("Listener onUpdate Executed");
    });

    chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab) {
        findKeyword();
        console.log("Listener onActivated Executed");
    });

    chrome.tabs.onCreated.addListener(function(tab) {         
       findKeyword();
       console.log("Listener onCreated Executed");

    });


    // iFrame Listeners    
    var iframe_id = res.obj.iframe_id;
    //var iframe = document.getElementById(res.obj.iframe_id);      // alternative

    $(iframe_id).find("body").on('mouseup', function(event) {
        findKeyword();
        console.log("iFrame Listener - MouseUp");
    });

    $('iframe#com.ihg.crm.web.hotel.Hotel').load(function() {
      findKeyword();
      alert("iFrame Listener - Load");
    });

    iframe_id.contentDocument.body.addEventListener('change', function() {
        findKeyword();
        console.log("iFrame Listener - body changes");
    });


    // MutationObservers
    var dom_observer = new MutationObserver(function(mutation) {
        findKeyword();
        console.log('Mutation observed');
    });

});  

此外,我最近遇到一个Error handling response: TypeError: Cannot read property 'onUpdated' of undefined错误,这很奇怪,因为我正在从后台脚本中触发代码,而AFAIK仅在所有内容加载完毕后才加载。

感谢您的帮助。

0 个答案:

没有答案