Google在同一页面中多次调用runtime.js时出错 [英] error with google swiffy calling runtime.js multiple times in the same page

查看:75
本文介绍了Google在同一页面中多次调用runtime.js时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have converted multiple swf files using google swiffy v5.2 and will have my new animations displayed on many different pages, most of which I do not have control of or access to. In order for the animation to work it needs the swiffy's runtime.js file, which might look something like this on the page:

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

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

The problem arises when I either have multiple instances of the animation on the same page or a client has this runtime.js file included on their own. When checking the javascript console I get this error:

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

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

If i was only worried about the conflict with myself I could possibly keep track of a variable or check if the script src existed already, however I do not have this luxury when a client's page may have renamed or changed the source to this file.

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

Is there a way to prevent the swiffy runtime.js from redefining this property when there are multiple instances of the same javascript file being included on the page?

推荐答案

我想您在使用AS3 swfs时会遇到此问题,该AS3 swfs应用了Document类.例如,假设您有animationAS3.swf,它使用AnimationBaseClass.as.当Google Swiffy服务对其进行编译"时,生成的JSON数据将包含 {"internedStrings":["...", "AnimationBaseClass", "..."] ....}

I imagine you are seeing this problem happen when using AS3 swfs, which have Document classes applied to them. For example, say you have animationAS3.swf, which uses AnimationBaseClass.as. When it is "compiled" by Google Swiffy service the resultant JSON data will contain {"internedStrings":["...", "AnimationBaseClass", "..."] ....}

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

The Google Swiffy runtime applies JavaScript's defineProperties() or perhaps defineProperty() to seal an "AnimationBaseClass" object it creates. So, when another instance of the data is loaded the Swiffy runtime attempts to do the same thing again, and the JavaScript interpreter says "Hey, I've already defined that object, I won't redefine it."

我发现的解决方案(在我看来效率不高)是在将数据提供给Swiffy运行时之前重命名该类.像这样:

The solution I've found, which I believe is inefficient, is to rename the class before giving the data to the Swiffy runtime. Like this:

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++));
}

这篇关于Google在同一页面中多次调用runtime.js时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆