Greasemonkey Uncaught ReferenceError: initScript is not defined in Chrome only
The code below, Greasemonkey script, works in Firefox and Opera. Also when packaged for Safari it works fine. When run in Chrome however I get Uncaught ReferenceError: initScript is not defined. Everything seems to work fine right up until initScript() is called. jQuery successfully loads and the setWide() and setHigh() functions work correctly.
If I move the initScript() function inside of preparePage() then it works fine. I'm not sure why that is necessary though.
I have the script开发者_StackOverflow社区 wrapped in an anonymous function so I could set "use strict" once for the entire script. I have tried running the script without "use strict" and also without being wrapped. No change.
Any advice would be greatly appreciated.
MORE INFO: I commented out all the code within each function and just put in a console.log message at the beginning of each function.
function initScript() {
console.log('initScript');
}
If I do that for every function then each function is run in order as it should be. I'm wondering if the way jQuery is loaded might be the issue.
// ==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();
}
}());
POSSIBLE SOLUTION: Removed the anonymous function. Place the initScript() and preparePage() function inside another function called main(). Change addLibrary(preparePage) to addLibrary(main). Added a call to preparePage() at the bottom of the main() function. That seems to work in Chrome. Not tested elsewhere yet. Will post the fixed code if/when I'm happy with it. Not sure if this is the best way to go yet though. Any input is always appreciated :)
精彩评论