简体   繁体   English

Chrome扩展程序:如何修改从网页中的Ajax请求创建的元素?

[英]Chrome extension: How to modify elements created from an Ajax request in a webpage?

I'm trying to make a chrome extension for this page http://jisho.org/kanji/radicals/ and I've got a problem. 我正在尝试为此页面http://jisho.org/kanji/radicals/制作Chrome扩展程序,我遇到了问题。

The extension should modify the elements containing "kanji" found in the #found_kanji div , but those elements are not there when the page loads, rather, they are created after the user clicks a "radical"(a part of a kanji) which triggers an Ajax query that loads the kanji from the website database. 扩展名应该修改#found_kanji div包含“kanji”的元素,但是当页面加载时这些元素不存在,而是在用户点击“激进”(汉字的一部分)后触发它们从网站数据库加载汉字的Ajax查询。

How can I modify those elements? 我该如何修改这些元素?

From what I've looked up, DOM manipulation events are now deprecated, so I can't watch for when the elements are created. 从我查看的内容来看,DOM操作事件现在已被弃用,因此我无法监视元素的创建时间。 I could try to replace the website javascript with a local version by using the webRequests API, I thought it would be ugly but it could work, but then I realized I have no way to communicate from a webpage script to a content script or the extension itself. 我可以尝试使用webRequests API替换本地版本的网站javascript,我认为它会很难看但它可以工作,但后来我意识到我无法从网页脚本到内容脚本或扩展程序进行通信本身。 Anyone got ideas? 有人有想法吗?

You can use a MutationObserver to listen for DOM changes within a particular div, and perform your modifications whenever new elements are added to the div: 您可以使用MutationObserver监听特定div中的DOM更改,并在将新元素添加到div时执行修改:

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    if(mutation.addedNodes.length){
        var addedNodes = Array.prototype.slice.call(mutation.addedNodes);
        addedNodes.forEach(function(node){
            if(node.className == 'something'){
                // do stuff
            }
        });
    }
  });    
});

observer.observe(document.getElementById('found_kanji'), { subtree: true, childList: true, characterData: true });

jsFiddle demo jsFiddle演示

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

相关问题 如何通过Chrome扩展程序修改请求正文 - How to modify request bodies by chrome extension Chrome扩展程序:将javascript注入网页以执行ajax请求? - Chrome extension: Inject javascript into a webpage to perform a ajax request? Chrome扩展程序中的Ajax请求 - Ajax request in Chrome extension 如何通过Chrome扩展程序从网页中使用Node的请求库? - How can I use Node's request library from within a webpage through a Chrome Extension? Chrome扩展程序-如何获取从网页调用的POST请求用户的响应? - Chrome extension - how to get response of POST request user invoked from a webpage? 如何根据 Chrome 扩展中的用户选项修改 http 请求标头? - How to modify http request headers based on user options in a Chrome extension? 如何拦截来自网页的每个AJAX请求 - How to intercept every AJAX request from a webpage 如何将用户信息从服务器传递到Chrome扩展的网页? - How to pass user information from server to webpage for a chrome extension? 如何通过将侦听器添加到“窗口”来从Chrome扩展程序单击网页上的按钮? - How to click a button on a webpage from Chrome extension by adding a listener to `window`? 无法通过Chrome扩展程序的Ajax请求将值存储在数据库中 - Unable to store values in database through an Ajax request from chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM