【发布时间】:2020-12-27 07:13:05
【问题描述】:
我正在编写一个 chrome 扩展程序来检测在this website 上单击某个按钮后 DOM 中的变化。具体来说,一旦单击添加到购物车按钮,就会出现一个显示更多信息的弹出窗口,并且当这种情况发生时,DOM 会发生变化。我编写了一个事件侦听器来查看何时单击添加到购物车按钮。但是,如果我单击其中一个按钮来指定我的大小,控制台中会出现一条通知,说明 Overriding "availability.update"!。在此之后,当我单击添加到购物车按钮时,我的代码没有任何响应。有谁知道如何解决这个问题?
JavaScript (content.js)
document.getElementById("add-to-cart").addEventListener("click", domChange); //event listener for add-to-cart button
function domChange(){ //checks for changes in the DOM
var myElement = $('<div class="modal-content js-modal-content></div>')[0];
var observer = new MutationObserver(function(mutations) {
if (document.contains(myElement)) {
console.log("It's in the DOM!");
observer.disconnect();
}else{
console.log('did not work');
}
});
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});
}
Manifest.json
{
"manifest_version": 2,
"name": "Chrome Extension Test",
"description": "chrome extension trial build",
"version": "1.0.0",
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [{
"js": ["content.js"],
"matches": ["http://*/*", "https://*/*"]
}],
"content_security_policy": "script-src 'self' 'unsafe-eval' https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js; object-src 'self'",
"permissions": [
"http://localhost/",
"tabs",
"<all_urls>"
]
}
HTML (popup.html)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="content.js"></script>
</head>
<body>
</body>
</html>
【问题讨论】:
标签: javascript jquery dom events web-scraping