Code works in Scratchpad but not in content script?


(Diracsbracket) #1

Hi,
I am completely new to add-ons and scripting, so please forgive me this basic question.

I am trying to achieve the following: When I open my particular web page containing a radio stream (using jwplayer), I simply want to start the player automatically, which it does not do by default.

So, I thought about using a content script that runs after the load event of the page, and then issue the play command, like so:

jwplayer('some-internet-player').play(true);

The above command works when executed in the Scratchpad, but not when I test it in my very basic content script (I added a delay, but that does not help, no matter what the delay
value is):

console.log("TEST MY CONTENT SCRIPT");

window.addEventListener ("load", myMain, false);

function myTest() {
	console.debug("myTest()")
	jwplayer('some-internet-player').play(true);
}

function myMain (evt) {
console.debug("TEST ON LOAD EVENT **************");
setTimeout(myTest, 6000); 

I can see the debug statements fine in the debugger console, but I can’t seem to access the jwplayer object the way it works in the Scratchpad.

What am I missing here?

Thanks for any help!


(Martin Giger) #2

See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#Accessing_page_script_objects_from_content_scripts


(Diracsbracket) #3

Thanks a lot!

As per the info in the link, I instead used:

wrappedJSObject.jwplayer('some-internet-player').play(true);

Cheers!