【问题标题】:error with google swiffy calling runtime.js multiple times in the same pagegoogle swiffy 在同一页面中多次调用 runtime.js 时出错
【发布时间】:2013-07-07 19:11:44
【问题描述】:

我已经使用 google swiffy v5.2 转换了多个 swf 文件,并且我的新动画将显示在许多不同的页面上,其中大部分我无法控制或访问。为了让动画正常工作,它需要 swiffy 的 runtime.js 文件,它在页面上可能看起来像这样:

 <script src="https://www.gstatic.com/swiffy/v5.2/runtime.js"></script>

当我在同一页面上有多个动画实例或客户端自己包含此 runtime.js 文件时,就会出现问题。检查 javascript 控制台时,我收到此错误:

 Uncaught TypeError: Cannot redefine property: __swiffy_override - runtime.js:186

如果我只是担心与自己的冲突,我可能会跟踪变量或检查脚本 src 是否已经存在,但是当客户的页面可能已重命名或将源更改为此时,我没有这种奢侈文件。

当页面上包含同一个 javascript 文件的多个实例时,是否有办法防止 swiffy runtime.js 重新定义此属性?

【问题讨论】:

    标签: javascript javascript-objects google-swiffy


    【解决方案1】:

    我想你在使用 AS3 swfs 时会看到这个问题,它应用了 Document 类。例如,假设您有使用 AnimationBaseClass.as 的 animationAS3.swf。当它由 Google Swiffy 服务“编译”时,生成的 JSON 数据将包含 {"internedStrings":["...", "AnimationBaseClass", "..."] ....}

    Google Swiffy 运行时应用 JavaScript 的 defineProperties() 或 defineProperty() 来密封它创建的“AnimationBaseClass”对象。因此,当加载另一个数据实例时,Swiffy 运行时尝试再次执行相同的操作,并且 JavaScript 解释器说“嘿,我已经定义了该对象,我不会重新定义它。”

    我认为效率低下的解决方案是在将数据提供给 Swiffy 运行时之前重命名类。像这样:

    var classEnumerator = 0; 
    $.getJSON('animationAS3.json', function(data) {
        // Due to "TypeError: Cannot redefine property: AnimationBaseClass", 
        // we need to enumerate the name of the class.  I have no idea about 
        // the impact on resource usage when doing this.
    
        var classNameIndex;
        var i = data.internedStrings.length;
        while(i--) {
            if (data.internedStrings[i].indexOf("AnimationBaseClass") > -1) {
                classNameIndex = i;
            }
        }
    
        data.internedStrings[classNameIndex] = "AnimationBaseClass_" + (classEnumerator++));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      相关资源
      最近更新 更多