SWFobject in a Chrome Extension - API Unavaiable
Asked Answered
C

2

7

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

Coronary answered 1/9, 2011 at 17:56 Comment(9)
No answer for you, but a suggestion: your YouTube SWFObject code declares FlashVars in two different places; I suggest you simplify to a single method. ?enablejsapi=1&playerapiid=player can be inserted into the FlashVars object as { autoplay: 1, enablejsapi: 1, playerapiid: "player" }. Alternately, you can move 'autoplay' into the querystring: ?enablejsapi=1&playerapiid=player&autoplay=1Cookhouse
@Cookhouse Thanks for the suggestion, I don't know how I forgot that!Coronary
I'm confused about how "player" is defined. Sometimes its a global (in "playerPause") and sometimes it's a local (in "onYouTubePlayerReady") maybe there's a hint there.Behead
@Behead You are right, it was a little confusing, so I changed the values to the default ones in their tutorial (I am desperate), but it is still not working. The onYouTubePlayerReady function never gets fired, and even if I fire it from the console I get that the methods are undefined...Coronary
My fear is: (From the documentation) Note: To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet. Which would mean that the Youtube player can't be embedded in a Chrome extension. Can anyone confirm?Coronary
You can test the local security by changing your Flash Player security settings to allow local communication. Specifically, you have to allow access to local folders. Here's a decent write up of how to do that: krpano.com/docu/localusage If it works after you change your security settings, you've found the problem. :)Cookhouse
Did you ever come to a conclusion about this? I am experiencing a similar issue. I am able to get it to work without using the swfobject, but deployment is a whole 'nother can of worms.Economize
Have you tried version 2 of manifest file (Chrome 18 and above) with proper content permissions?September
Why do you want to embed YouTube video to background page? Background page is something not visible to user. From what I know, you can not embed youtube video to extensions pages. However, you can embed youtube video to sandboxed page. developer.chrome.com/extensions/manifest.html#sandboxFallfish
R
0

You can have a look at this extension, you can not access local connection in chrome extension, but you can run a content script as a proxy script instead.(You can serve a proxy page on gae or any other free servers)

Rm answered 19/6, 2012 at 0:27 Comment(0)
S
0

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.

Subdivision answered 3/8, 2013 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.