JavaScript block CSS

JavaScript block CSS

本文关键字:CSS block JavaScript      更新时间:2023-09-26

我有一个.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之间的代码。所以只有addDivremoveDiv。我稍微修改了上面的代码片段,所以当你调用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)}});'    
});