我有一個奇怪的問題。 我嘗試編寫一個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