JavaScript block CSS
JavaScript block CSS
我有一个.js文件,当使用GreaseMonkey时,它可以很好地阻止所有CSS代码,但我也需要它作为。xpi (Firefox扩展)运行,它不能很好地工作。
我已经尝试了很多事情,现在我正试图用Firefox扩展生成器SDK调试它,所以我得到了所有的错误信息。下面是我得到的错误信息:
console.error: css3blocker:
Message: ReferenceError: document is not defined
,这里是。js代码:
[].slice.call(document.styleSheets) .forEach(function (sheet) {
try {
[].slice.call(sheet.cssRules) .forEach(function (rule) {
if (rule.media) {
rule.media.mediaText = '';
}
});
} catch (err) {
console.log(err);
}
});
这个错误是什么意思?
BIG BIG BIG BIG BIG BIG感谢Noitidart!!!!!
你帮了我很多!
现在得到它的工作,xpi是有点更积极的JS在GreaseMonkey,但它现在工作!
把JS改成这样:
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*",
contentScript: '[].slice.call(document.styleSheets) .forEach(function (sheet) {try {[].slice.call(sheet.cssRules) .forEach(function (rule) {if (rule.media) {rule.media.mediaText='''';}});} catch (err) {console.log(err);}});'
});
它可以在XPI Builder上工作!: D
方法1 - preference
要阻止CSS从插件范围,可能有一个about:config首选项来禁用它。我不确定是否有偏好,所以除非有人告诉你有,否则请使用以下方法。
方法2 -修改制表符
仅在不希望阻塞css的情况下使用。可以复制粘贴到记事本。
这里唯一需要注意的部分是//add your stuff here
和//end add your stuff here
之间的代码。所以只有addDiv
和removeDiv
。我稍微修改了上面的代码片段,所以当你调用windowListener.unregister()
时,removeDiv
将恢复css。
你必须更新这段代码才能使用,一旦firefox多进程的选项卡。
var {interfaces: Ci, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var ignoreFrames = false;
function addDiv(theDoc) {
if (!theDoc) { Cu.reportError('no doc!'); return; } //document not provided, it is undefined likely
removeDiv(theDoc, true); //remove my div if it was already there, this is just a precaution
//add your stuff here
[].slice.call(theDoc.styleSheets).forEach(function(sheet) {
try {
[].slice.call(sheet.cssRules).forEach(function(rule) {
if (rule.media) {
rule.media.mediaText = '/*' + escape(rule.media.mediaText) + '*/';
}
});
} catch (err) {
console.log(err);
}
});
//end add your stuff here
}
function removeDiv(theDoc, skipChecks) {
if (!skipChecks) {
if (!theDoc) { Cu.reportError('no doc!'); return; } //document not provided, it is undefined likely
}
//add your stuff here
[].slice.call(theDoc.styleSheets).forEach(function(sheet) {
try {
[].slice.call(sheet.cssRules).forEach(function(rule) {
if (rule.media) {
rule.media.mediaText = unescape(rule.media.mediaText.substr(2, rule.media.mediaText.length-2));
}
});
} catch (err) {
console.log(err);
}
});
//end add your stuff here
}
function listenPageLoad(event) {
var win = event.originalTarget.defaultView;
var doc = win.document;
Cu.reportError('page loaded loc = ' + doc.location);
if (win.frameElement) {
//its a frame
Cu.reportError('its a frame');
if (ignoreFrames) {
return;//dont want to watch frames
}
}
addDiv(doc);
}
/*start - windowlistener*/
var windowListener = {
//DO NOT EDIT HERE
onOpenWindow: function (aXULWindow) {
// Wait for the window to finish loading
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
aDOMWindow.addEventListener("load", function () {
aDOMWindow.removeEventListener("load", arguments.callee, false);
windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
}, false);
},
onCloseWindow: function (aXULWindow) {},
onWindowTitleChange: function (aXULWindow, aNewTitle) {},
register: function () {
// Load into any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.loadIntoWindow(aDOMWindow, aXULWindow);
}
// Listen to new windows
Services.wm.addListener(windowListener);
},
unregister: function () {
// Unload from any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.unloadFromWindow(aDOMWindow, aXULWindow);
}
//Stop listening so future added windows dont get this attached
Services.wm.removeListener(windowListener);
},
//END - DO NOT EDIT HERE
loadIntoWindow: function (aDOMWindow, aXULWindow) {
if (!aDOMWindow) {
return;
}
if (aDOMWindow.gBrowser) {
aDOMWindow.gBrowser.addEventListener('DOMContentLoaded', listenPageLoad, false);
if (aDOMWindow.gBrowser.tabContainer) {
//has tabContainer
//start - go through all tabs in this window we just added to
var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
for (var i = 0; i < tabs.length; i++) {
Cu.reportError('DOING tab: ' + i);
var tabBrowser = tabs[i].linkedBrowser;
var win = tabBrowser.contentWindow;
loadIntoContentWindowAndItsFrames(win);
}
//end - go through all tabs in this window we just added to
} else {
//does not have tabContainer
var win = aDOMWindow.gBrowser.contentWindow;
loadIntoContentWindowAndItsFrames(win);
}
} else {
//window does not have gBrowser
}
},
unloadFromWindow: function (aDOMWindow, aXULWindow) {
if (!aDOMWindow) {
return;
}
if (aDOMWindow.gBrowser) {
aDOMWindow.gBrowser.removeEventListener('DOMContentLoaded', listenPageLoad, false);
if (aDOMWindow.gBrowser.tabContainer) {
//has tabContainer
//start - go through all tabs in this window we just added to
var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
for (var i = 0; i < tabs.length; i++) {
Cu.reportError('DOING tab: ' + i);
var tabBrowser = tabs[i].linkedBrowser;
var win = tabBrowser.contentWindow;
unloadFromContentWindowAndItsFrames(win);
}
//end - go through all tabs in this window we just added to
} else {
//does not have tabContainer
var win = aDOMWindow.gBrowser.contentWindow;
unloadFromContentWindowAndItsFrames(win);
}
} else {
//window does not have gBrowser
}
}
};
/*end - windowlistener*/
function loadIntoContentWindowAndItsFrames(theWin) {
var frames = theWin.frames;
var winArr = [theWin];
for (var j = 0; j < frames.length; j++) {
winArr.push(frames[j].window);
}
Cu.reportError('# of frames in tab: ' + frames.length);
for (var j = 0; j < winArr.length; j++) {
if (j == 0) {
Cu.reportError('**checking win: ' + j + ' location = ' + winArr[j].document.location);
} else {
Cu.reportError('**checking frame win: ' + j + ' location = ' + winArr[j].document.location);
}
var doc = winArr[j].document;
//START - edit below here
addDiv(doc);
if (ignoreFrames) {
break;
}
//END - edit above here
}
}
function unloadFromContentWindowAndItsFrames(theWin) {
var frames = theWin.frames;
var winArr = [theWin];
for (var j = 0; j < frames.length; j++) {
winArr.push(frames[j].window);
}
Cu.reportError('# of frames in tab: ' + frames.length);
for (var j = 0; j < winArr.length; j++) {
if (j == 0) {
Cu.reportError('**checking win: ' + j + ' location = ' + winArr[j].document.location);
} else {
Cu.reportError('**checking frame win: ' + j + ' location = ' + winArr[j].document.location);
}
var doc = winArr[j].document;
//START - edit below here
removeDiv(doc);
if (ignoreFrames) {
break;
}
//END - edit above here
}
}
现在复制粘贴后。windowListener.register();
和删除/恢复css调用windowListener.unregister();
建筑解决方案
pageMod.PageMod({
include: "*",
contentScript: '[].slice.call(document.styleSheets)forEach(function(sheet){try{[].slice.call(sheet.cssRules).forEach(function(rule){if(rule.media){rule.media.mediaText=''}})}catch(err){console.log(err)}});'
});
相关文章:
- CSS-如何定位内容数据标题
- 窗口大小html css
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 动画.CSS重播
- 有没有一种方法可以防止img get请求使用css或js发生
- CSS表格:从列平移到整个表格宽度
- CKEditor-我在editor.css中的风格是't
- 单击更改图标并通过javascript添加一个css类
- 将CSS应用于printWindow.print();在Javascript中
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- css(或jQuery)悬停时淡入淡出
- 为什么不'在JQuery中找到第二个css选择器的工作
- Javascript -> CSS (display:none to display:block)
- CSS: jQuery:将右边距应用于 li 元素,除了每个第 4 个子元素(仅适用于 display:block 的元
- JS监视CSS属性的变化,如“display:none”=>“display:block”
- 如何.css('display')是block,[0].hidden是false,.is(':h
- JavaScript block CSS
- if($(var).css('display') == "block") with
- CSS过渡不透明度不工作的元素有display:none,然后改变为display:block