开发者

Replacing a JavaScript function with a UserStyle

I'm working on modifying a UserStyle for Instapaper. Since the original UserStyle was written, Instapaper added to their header a number of JavaScript functions that control the width of the page and the typefaces used.

Here they are:

        functio开发者_Python百科n loadDefaults()
        {
            _fontSize = 16;
            _fontFamily = "G";
            _lineHeight = 1.5;
            _width = 500;
        }

        function loadFont()
        {
            var cookieData = readCookie("fontMetrics");
            if (cookieData && (cookieData = cookieData.split("_")) && cookieData.length == 4) {
                _fontSize = parseInt(cookieData[0]);
                _fontFamily = cookieData[1];
                _lineHeight = parseFloat(cookieData[2]);
                _width = parseInt(cookieData[3]);
            } else loadDefaults();

            applyFont();
        }

How would I go about modifying the UserScript to override these functions, since they execute after load?

So far, I've tried simply replacing these functions with blank overrides, but it didn't work. Since my script executes first, is there some way I can just remove the entire JavaScript block in the header?


First of all, userscripts are for javascript not userstyles. Secondly you want to use unsafeWindow in your user script in order to overwrite functions defined in the window scope by the webpage.

So your code would look like:

    unsafeWindow.loadDefaults = function loadDefaults()
    {
        _fontSize = 16;
        _fontFamily = "G";
        _lineHeight = 1.5;
        _width = 500;
    }

    unsafeWindow.loadFont = function loadFont()
    {
        var cookieData = readCookie("fontMetrics");
        if (cookieData && (cookieData = cookieData.split("_")) && cookieData.length == 4) {
            _fontSize = parseInt(cookieData[0]);
            _fontFamily = cookieData[1];
            _lineHeight = parseFloat(cookieData[2]);
            _width = parseInt(cookieData[3]);
        } else loadDefaults();

        applyFont();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜