javascript - Google Chrome extension run as if in console? - Stack Overflow

I'm trying to make a Google Chrome extension that pausesplays the payer in DEEZER (www.deezer). I

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?

Share Improve this question edited Nov 6, 2012 at 18:04 apsillers 116k18 gold badges247 silver badges247 bronze badges asked Nov 6, 2012 at 17:42 user1803920user1803920 1162 silver badges6 bronze badges 6
  • 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
 |  Show 1 more ment

2 Answers 2

Reset to default 6

The problems that you are facing here are the following:

  1. You need to use content_scripts instead of running code from the background page.
  2. 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信