2011-10-13 19 views
2

下面的代码是Greasemonkey脚本,适用于Firefox和Opera。另外,当为Safari打包时,它工作正常。在Chrome中运行时,我得到Uncaught ReferenceError:没有定义initScript。在initScript()被调用之前,一切似乎都正常工作。 jQuery成功加载并且setWide()和setHigh()函数正常工作。Greasemonkey未捕获ReferenceError:未在Chrome中定义initScript

如果我在preparePage()中移动initScript()函数,那么它工作正常。我不确定为什么这是必要的。

我把脚本包装在一个匿名函数中,所以我可以为整个脚本设置“use strict”一次。我曾尝试运行脚本而没有“严格使用”,也没有被包装。不用找了。

任何意见将不胜感激。

更多信息:我注释掉了每个函数中的所有代码,并在每个函数的开头放置了一个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(); 
    } 
}()); 

回答

0

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 :)

相关问题