我有一个奇怪的问题。 我尝试编写一个GreaseMonkey脚本,以便在Firefox和Google Chrome中运行。 在Chrome中,我尝试了两个扩展名:“TamperMonkey”和“空白画布脚本处理程序”,主要是因为我的脚本在外部网站上对新版本进行了定期检查,这被认为是跨站点脚本,未经Chrome授权。Chrome无法读取style.display值
要告诉你我的问题,我写了一个简单的测试案例:
// ==UserScript==
// @name test
// @namespace http://fgs.ericc-dream.fr.nf
// @description test gm script
// @include http://gaia.fallengalaxy.eu/
// @author ericc
// @version 0.0.1
// ==/UserScript==
/* We attach an event listener to the body tag and trigger the function
* 'message' each time that an element is inserted in the page */
var el = document.body;
el.addEventListener('DOMNodeInserted', message, false);
var extraFlag = false;
function message(event) {
/* first we capture the id of the new inserted element
* (the one who created the event) */
var objId = event.target.id;
/* add an event listener on the map container */
if (objId == "extra") {
el = document.getElementById('extra');
el.addEventListener('DOMSubtreeModified',readTest,false);
GM_log(el.style.display);
}
}
function readTest() {
el = document.getElementById('extra');
GM_log(extraFlag);
GM_log(el.style.display);
if ((el.style.display != 'none') && (!extraFlag)) {
alert('extra');
extraFlag = true;
} else if ((el.style.display == 'none')) {
extraFlag = false;
}
}
的div元素“额外”通过网页进行修改。问题在于Chrome无法读取el.style.display的值,因此extraFlag再也不会变成'false'。 我使用这个标志避免了几次运行代码,该网站是严重的JavaScript驱动 这段代码在Firefox中很好地工作!
我试图用Google搜索但找不到正确的答案。似乎很容易改变显示的价值,但似乎我是唯一一个试图阅读它的人!
我写这个代码,因为“DOMAttrModified”是在Chrome事先不支持:-(
感谢您的帮助
ericc
有关问题的其他信息不应该作为答案发布。编辑您的原始问题以包含此信息。 – 2011-03-31 13:27:55