Chrome扩展程序中的SWF对象 - API Unavaiable [英] SWFobject in a Chrome Extension - API Unavaiable

查看:168
本文介绍了Chrome扩展程序中的SWF对象 - API Unavaiable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在构建Chrome扩展程序,其中需要在后台页面中嵌入SWF对象。

一切正常,除了SWFobject和eventListeners的JavaScript控件。

我的猜测是它与跨域策略有关,因为在web服务器上测试页面时一切正常。



无论如何,这里有一个片段:



在主页面:

  var playerView = chrome.extension.getBackgroundPage(); 
$('#playerPause')。click(function(){
playerView.playerPause();
});

在后台:

  function playerPause(){
if(postData [nowPlaying] .provider =='youtube'){
player.pauseVideo();
}
else if(postData [nowPlaying] .provider =='soundcloud'){
player.api_pause();
};
}

eventListeners

  soundcloud.addEventListener('onMediaEnd',playerNext); 

功能onYouTubePlayerReady(播放器){
player.addEventListener( onStateChange, 功能(状态){如果(状态== 0){playerNext();}});





在控制台中它会抛出


Uncaught TypeError:Object#has no method
'pauseVideo'


为这两个Youtube嵌入了Soundcloud。

另外, SWFobject 就像这样嵌入( ):

 函数loadTrack(id){
if(postData [id] .provider =='youtube') {
swfobject.embedSWF(
http://www.youtube.com/e/+ postData [id] .url +?enablejsapi = 1& playerapiid = player,
玩家,
1,
1,
8,
null,
{
自动播放:1
},
{
allowScriptAccess:always
},
{
id:player
}
);
}
else if(postData [id] .provider =='soundcloud'){
swfobject.embedSWF(
'http://player.soundcloud.com/player。 swf',
'player',
'1',
'1',
'9.0.0',
'expressInstall.swf',
{
enable_api:true,
object_id:'player',
url:postData [id] .url,
auto_play:true
},
{
allowcriptaccess:'always'
},
{
id:'player',
name:'player'
}
) ;


$ / code $ / pre

对于冗长的文章,我想提供尽可能多的信息。

另外,我知道代码并不漂亮,这只是我的第二个应用程序;)



非常感谢提前给任何人谁可以帮助,

Giacomo

解决方案

这里的问题是,你不能使用内联脚本或内联事件处理程序,自从清单发展到v2以来一直是chrome扩展中的一部分。



您应该添加清单文件以便我更好地理解所发生的情况。但简单地说所有可以做的都是做的是

在主页面中,

删除所有内联脚本并将它们移动到外部JS文件中。 删除内联事件监听器,将它们移动到相同或另一个外部JS并使用


addEventListener()。

但问题是, t在后台页面中执行对swf的调用,或期望它返回任何内容。所有这些将继续给你Uncaught TypeError例外。


在网络摄像头图像捕获swf的情况下,网络摄像头将流式传输到页面,但不能进行函数调用,因此图像将永远不会被捕获。
我的项目从附件弹出窗口扫描QR码时遇到了遗漏。


Hi!
I'm building a Chrome extension, in which I need to embed a SWFobject in the background page.
Everything works, except the JavaScript controls for the SWFobject and the eventListeners.
My guess is that it has something to do with the cross-domain policies, because while testing the page on a webserver everything worked fine.

Anyway, here's a snippet:

In the main page:

var playerView =  chrome.extension.getBackgroundPage(); 
$('#playerPause').click(function(){
    playerView.playerPause();
});

In the background:

function playerPause() {
    if (postData[nowPlaying].provider == 'youtube' ) {
        player.pauseVideo();
    } 
    else if (postData[nowPlaying].provider == 'soundcloud' ) {
        player.api_pause();
    };
}

And the eventListeners:

soundcloud.addEventListener('onMediaEnd', playerNext);

function onYouTubePlayerReady(player) {
    player.addEventListener("onStateChange", "function(state){ if(state == 0) { playerNext(); } }");
}

In the console it throws

"Uncaught TypeError: Object # has no method 'pauseVideo'"

for both the Youtube embed the Soundcloud one.

Also, the SWFobject is embedded like this (and works):

function loadTrack (id) {
    if(postData[id].provider == 'youtube') {
        swfobject.embedSWF(
            "http://www.youtube.com/e/" + postData[id].url + "?enablejsapi=1&playerapiid=player",
            "player",
            "1",
            "1",
            "8",
            null,
            {
                autoplay: 1
            },
            {
                allowScriptAccess: "always"
            },
            {
                id: "player"
            }
        );
    }
    else if(postData[id].provider == 'soundcloud') {
        swfobject.embedSWF(
            'http://player.soundcloud.com/player.swf',
            'player',
            '1',
            '1',
            '9.0.0',
            'expressInstall.swf',
            {
                enable_api: true, 
                object_id: 'player',
                url: postData[id].url,
                auto_play: true
            },
            {
                allowscriptaccess: 'always'
            },
            {
                id: 'player',
                name: 'player'
            }
        );
    }
}

Sorry for the lengthy post, I wanted to provide as much information as possible.
Also, I know the code isn't pretty, this was only my second application ;)

Thanks a lot in advance to anyone who can help,
Giacomo

解决方案

The problem here is that you can't use inline scripts or inline event handlers in chrome extensions ever since the manifest evolved to v2.

You should have added the manifest file for me to understand what's going on better. But briefly all you CAN ever do is

In the main page,

  • Remove all inline scripts and move them to an external JS file.
  • Remove inline event listeners, move them to the same or another external JS and use

    addEventListener().

But the issue is, You can't execute calls to the swf in the background page or expect it to return anything. All these will continue to give you "Uncaught TypeError" Exception.

Take the case of a webcam image capturing swf, the webcam will be streamed to the page, but the function call to it can never be made and hence the image will never be captured. My project to scan QR codes from the addons popup met the ruins due to this.

这篇关于Chrome扩展程序中的SWF对象 - API Unavaiable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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