I'm trying to make a Google Chrome extension that pauses/plays the payer in DEEZER (www.deezer). I i manually run the code "playercontrol.doAction('next')
" in the Google chrome JavaScript console, I can manipulate the deezer player, so i looked at the possibility of injecting the code into the Deezer web page with an extension but I haven't managed to do so successfully.
Is it possible to run JavaScript code in Deezer web page as if it were ing from the console in Google Chrome?
If so how?
I'm trying to make a Google Chrome extension that pauses/plays the payer in DEEZER (www.deezer.). I i manually run the code "playercontrol.doAction('next')
" in the Google chrome JavaScript console, I can manipulate the deezer player, so i looked at the possibility of injecting the code into the Deezer web page with an extension but I haven't managed to do so successfully.
Is it possible to run JavaScript code in Deezer web page as if it were ing from the console in Google Chrome?
If so how?
- 1 Do you mean Java or JavaScript? Is there a Java applet somehow involved here? – apsillers Commented Nov 6, 2012 at 18:00
- Sorry, yes, i mean javaScript. – user1803920 Commented Nov 6, 2012 at 18:01
-
I suspect you're looking for either content scripts or
chrome.tabs.executeScript
. – apsillers Commented Nov 6, 2012 at 18:02 - Thank you very much, I'll research that. Do i execute the "chrome.tabs.executeScript" code from the background.js file or any other file declared in the manifest.json? – user1803920 Commented Nov 6, 2012 at 18:05
- 1 possible duplicate of Building a Chrome Extension - Inject code in a page using a Content script – Rob W Commented Nov 6, 2012 at 19:05
2 Answers
Reset to default 6The problems that you are facing here are the following:
- You need to use content_scripts instead of running code from the background page.
- Code run from the background page and code run as content scripts are "Sandboxed". This means that your code doesn't have access to the global variables created by the page. Your extension doesn't have access to it. THE GOOD NEWS IS that you can still do it. You have to be clever, though.
In your extension, you have to inject a new Script tag into the "head" of the page. You have to create a new script tag and append it to the head of the page. Before you append it, you need to set the "innerHTML" of the script tag to be the JavaScript that you want to run. Here is the gist of what you need to do.
var myJavaScript = "alert('hey guys');"; //You need to put your JS here.
var scriptTag = document.createElement("script");
scriptTag.innerHTML = myJavaScript;
document.head.appendChild(scriptTag);
This will put the new script tag into the head, which bypasses the security of the Chrome Extension. This will give the new script access to the regular global variables on the page, ie: you will have access to the 'playercontrol' object.
This feature has been deprecated by Chrome's security policy.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742299512a4417774.html
评论列表(0条)