在 Chrome 中,Flash 通过ExternalInterface 调用 JS 方法失败

发布于 2024-12-19 06:50:20 字数 3027 浏览 0 评论 0原文

我有以下问题。我在 HTML 页面中嵌入了一个 Flash 影片。有一个按钮,按下该按钮时,JavaScript 方法开始调用 Flash 内的函数。然后 Flash 通过调用 HTML 页面中的 JavaScript 方法进行响应。这个循环在 IE 和 FireFox 中运行良好。但它在 Google Chrome 中不起作用。经过一些调试,我发现应该从 Flash 调用的 JavaScript 方法永远不会被触发。我不明白为什么?这是我在 Flash 中的代码:

public static function update(transparent:Boolean = true, bgColor:Number = 0xFFFFFFFF, matrix:Matrix = null):void {
    if (!_enabled) return;
    if (_printSource == null) throw new Error("No source specified");
    if (!ExternalInterface.available) throw new Error("External Interface not available");

    var bitmap:BitmapData = new BitmapData(_printSource.width, _printSource.height, transparent, bgColor);
    bitmap.draw(_printSource, matrix,null,null,null,true);
    _base64encoder.reset();

    _base64encoder.encodeBytes(_pngEncoder.encode(bitmap));
    ExternalInterface.call("FlashPrintFix.setImg", ExternalInterface.objectID, _base64encoder.toString());
    }

如您所见,该方法调用 FlashPrintFix JS 文件中名为“setImg”的函数。

现在 JS 中的方法看起来像这样(尽管它的内容并不重要,因为它永远不会在 Chrome 中触发:

FlashPrintFix.setImg = function(objId, imgData) {

var obj = document.getElementById("graph");
     var obj =  document["graph"];
if (obj === null) {
    throw new Error("Can't find node with id '" + objId + "'");
}

obj.setAttribute("class", "printableFlashObj");

var container = obj.parentNode;
if (container.nodeName === "OBJECT") {
    container = container.parentNode;

}

var img = document.createElement("img");
img.setAttribute("class", "flashScreenshot");
img.src = "data:image/png;base64,"+imgData;
img.setAttribute("id", "imgPrnt");
    container.appendChild(img);
   $('imgPrnt').ready(function() {

          alert("Image is loaded");


        });};

这是 HTML 中的代码。因为它是 CakePHP 视图,所以默认情况下不包含所有常规 HTML 标签这些是在运行时由 PHP + 布局容器包装器生成的。但是以下代码负责 Flash 文件嵌入,并且在运行时驻留在 HTML 页面主体中:

<script type="text/javascript" >
 var currentUrl=<?php echo "'".XXXXXXX."'" ?>+'/XXX/XXXxXXXXXXX/';
$(document).ready(initApp());

 function initApp(){
  $("#flashContentWrapper").flash(
  {

        swf:currentUrl+<?php echo "'$app_filename'" ?>,
        id:"graph",
        name:"graph",
        allowScriptAccess:"always",
    height:600,
    width:1000,
    flashvars:{ 
        key:<?php echo "'$someKey'"?>
    }
  }
);

更新: 好吧,伙计们,放弃我的理论,即 JavaScript 不是通过 Flash 的外部接口调用的。实际问题出在 FlashPrintFix.js 中。对于所有使用该插件在 Flash 打印不支持的浏览器(如 Firefox 或 Chrome)中打印 Flash 内容的人来说,应该修复此问题脚本中的行:

 FlashPrintFix.needPrintFix = function () {
   var needF= /chrome|firefox|opera/i.test(navigator.userAgent);//<----Add "chrome" before "firefox"
   alert(needF);
   return  needF;


 };

问题是默认情况下 var needF 看起来像这样:

 var needF= /firefox|opera/i.test(navigator.userAgent);

对于无法打印 Flash 的浏览器,此变量返回 true,对于可以打印 Flash 的浏览器返回 false。因为默认情况下 chrome 不会被打印检查,因此这个 var 总是返回 true。如果它是 false,FlashPrintFix 的 Flash 类不会处理 JS 端的调用,因为它认为浏览器可以很好地打印 Flash 并且一切正常。感谢大家。

I have the following issue.I have a flash movie embedded in HTML page.There is a button which when pressed, a JavaScript method starts calling a function inside the Flash.Then Flash responds by calling a JavaScript method in the HTML page. This cycle works fine in IE and FireFox. But it doesn't work in Google Chrome.After some debugs I found that the JavaScript method that is supposed to get called from the Flash is never triggered.I can't get why? Here is my code in the Flash:

public static function update(transparent:Boolean = true, bgColor:Number = 0xFFFFFFFF, matrix:Matrix = null):void {
    if (!_enabled) return;
    if (_printSource == null) throw new Error("No source specified");
    if (!ExternalInterface.available) throw new Error("External Interface not available");

    var bitmap:BitmapData = new BitmapData(_printSource.width, _printSource.height, transparent, bgColor);
    bitmap.draw(_printSource, matrix,null,null,null,true);
    _base64encoder.reset();

    _base64encoder.encodeBytes(_pngEncoder.encode(bitmap));
    ExternalInterface.call("FlashPrintFix.setImg", ExternalInterface.objectID, _base64encoder.toString());
    }

As you can see this method calls A function called "setImg" from FlashPrintFix JS file.

Now the method in JS looks like this (although its content is not important because it is never triggered in the Chrome:

FlashPrintFix.setImg = function(objId, imgData) {

var obj = document.getElementById("graph");
     var obj =  document["graph"];
if (obj === null) {
    throw new Error("Can't find node with id '" + objId + "'");
}

obj.setAttribute("class", "printableFlashObj");

var container = obj.parentNode;
if (container.nodeName === "OBJECT") {
    container = container.parentNode;

}

var img = document.createElement("img");
img.setAttribute("class", "flashScreenshot");
img.src = "data:image/png;base64,"+imgData;
img.setAttribute("id", "imgPrnt");
    container.appendChild(img);
   $('imgPrnt').ready(function() {

          alert("Image is loaded");


        });};

And this is the code in HTML. Because it is a CakePHP view it doesn't contain all the regular HTML tags by default .Those are generated in runtime by PHP + layout container wrapper.But the following code is responsible for Flash File Embed and it resides in the HTML page body during runtime:

<script type="text/javascript" >
 var currentUrl=<?php echo "'".XXXXXXX."'" ?>+'/XXX/XXXxXXXXXXX/';
$(document).ready(initApp());

 function initApp(){
  $("#flashContentWrapper").flash(
  {

        swf:currentUrl+<?php echo "'$app_filename'" ?>,
        id:"graph",
        name:"graph",
        allowScriptAccess:"always",
    height:600,
    width:1000,
    flashvars:{ 
        key:<?php echo "'$someKey'"?>
    }
  }
);

UPDATE:
Ok guys, discard my theory that the JavaScript is not called via ExternalInterface from Flash.The actual problem was in the FlashPrintFix.js .For all those who use ths plugin for printing Flash content in Flash print unsupported browsers like Firefox or Chrome should fix this line in the script:

 FlashPrintFix.needPrintFix = function () {
   var needF= /chrome|firefox|opera/i.test(navigator.userAgent);//<----Add "chrome" before "firefox"
   alert(needF);
   return  needF;


 };

The problem is that by default var needF looks like this:

 var needF= /firefox|opera/i.test(navigator.userAgent);

The duty of this variable to return true for a browser that can't print Flash and false for the browser that can .Because by default chrome is not being checked and therefor this var always returns for it true.If it is false the Flash Class for FlashPrintFix doesn't process the calls on JS side because it thinks that the browser prints Flash fine and everything is all right. Thanks to all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文