【问题标题】:Greasemonkey Uncaught ReferenceError: initScript is not defined in Chrome onlyGreasemonkey Uncaught ReferenceError:initScript 仅在 Chrome 中未定义
【发布时间】:2011-12-06 14:29:43
【问题描述】:

下面的代码,Greasemonkey 脚本,适用于 Firefox 和 Opera。此外,当为 Safari 打包时,它也可以正常工作。但是,在 Chrome 中运行时,我得到 Uncaught ReferenceError: initScript is not defined。在调用 initScript() 之前,一切似乎都运行良好。 jQuery 成功加载并且 setWide() 和 setHigh() 函数正常工作。

如果我将 initScript() 函数移到 preparePage() 中,那么它可以正常工作。我不知道为什么这是必要的。

我将脚本包装在一个匿名函数中,因此我可以为整个脚本设置一次“使用严格”。我试过在没有“使用严格”的情况下运行脚本,也没有被包装。没有变化。

任何建议将不胜感激。

更多信息:我注释掉了每个函数中的所有代码,并在每个函数的开头添加了一条 console.log 消息。

function initScript() {
    console.log('initScript');
}

如果我对每个函数都这样做,那么每个函数都会按应有的顺序运行。我想知道 jQuery 的加载方式是否可能是问题。

// ==UserScript==
// @name            Testing Userscript
// @namespace       http://www.example.com/scripts
// @description     Cross browser testing
// @include         *://apps.facebook.com/exmaple/*
// @include         *://*.example.com/platforms/facebook/game
// @exclude         *://apps.facebook.com/example/rubies
// @match           *://apps.facebook.com/example/*
// @match           *://*.example.com/platforms/facebook/game
// @include         *://plus.google.com/games/example*
// @include         *://*.googleusercontent.com/gadgets/ifr?url=app://example*
// @match           *://plus.google.com/games/example*
// @match           *://*.googleusercontent.com/gadgets/ifr?url=app://example*
// @require         https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// @version         0.0.5
// ==/UserScript==
(function () {
    "use strict";

    function initScript() {
        var $J = jQuery.noConflict(), /* Change jQuery alias */
            OBJECT = "#swf_object", /* Page and browser specific constants */
            GLOBAL_VAR1, /* Global constants from object flashvars (see getFlashvars) */
            GLOBAL_VAR2;

        function getFlashvars() {
            var flashvars = $J(OBJECT + " param[name='flashvars']").attr("value").split("&"),
                keyValue,
                rslt = {};
            $J.each(flashvars, function () {
                keyValue = this.split("=");
                rslt[keyValue[0]] = keyValue[1];
            });
            GLOBAL_VAR1 = rslt.global_var1;
            GLOBAL_VAR2 = rslt.global_var2;
            alert(GLOBAL_VAR1);
        }

        getFlashvars();
    }

    function preparePage() {
        var iframe,
            $J = jQuery.noConflict(),
            object = "#swf_object",
            platform;

        function setHigh() {
            clearTimeout();
            if ($J(object).length < 1) {
                setTimeout(setHigh, 100);
                return;
            }
            switch (platform) {
            case "facebook":
                $J("#hd > div").css("display", "none");
                break;
            case "google":
                $J("#pane_hd").css("display", "none");
                break;
            }
            $J("#container").width("760px");
            initScript();
        }

        function setWide() {
            clearTimeout();
            if ($J(iframe).length < 1) {
                setTimeout(setWide, 100);
                return;
            }
            switch (platform) {
            case "facebook":
                $J("#rightCol").css("display", "none");
                break;
            }
            $J(iframe).parents().width("100%");
        }

        if (window.location.href.indexOf("facebook") !== -1) {
            iframe = "#iframe_canvas";
            platform = "facebook";
        } else if (window.location.href.indexOf("google") !== -1) {
            iframe = "#oz-gadgets-canvas-iframe-example";
            platform = "google";
        }

        if (window.top === window.self) {
            setWide();
        } else {
            setHigh();
        }
    }

    function addLibrary(callback) {
        var script = document.createElement("script");
        script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js");
        script.addEventListener('load', function () {
            var script = document.createElement("script");
            script.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
        }, false);
        document.body.appendChild(script);
    }

    if (typeof jQuery === "undefined") {
        addLibrary(preparePage);
    } else {
        preparePage();
    }
}());

【问题讨论】:

    标签: javascript jquery debugging cross-browser greasemonkey


    【解决方案1】:

    可能的解决方案:删除匿名函数。将 initScript() 和 preparePage() 函数放在另一个名为 main() 的函数中。将 addLibrary(preparePage) 更改为 addLibrary(main)。在 main() 函数的底部添加了对 preparePage() 的调用。这似乎在 Chrome 中有效。尚未在其他地方测试。如果/当我对它感到满意时,将发布固定代码。不过,不确定这是否是最好的方法。任何输入都会受到赞赏:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多