我希望使用制作一个铬扩展,它将连接到本地主机服务器,以接收在Grooveshark中播放歌曲的指令。(http://grooveshark.com/GroovesharkAPI.html)
例如,如果我在javascript控制台中输入window.Grooveshark.addSongsByID([13963],true),它会添加这首歌并开始播放,如果我需要从扩展中这样做的话。因此,首先,我只想用一个背景页面和这个执行命令的单个脚本来做一个扩展:
background.html
<!doctype html>
<html>
<head>
<script src="background.js"></script>
</head>
<body>
</body>
</html>background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {code:"window.Grooveshark.addSongsByID([13963],true)"});
});manifest.json
{
"name": "Play song in Grooveshark",
"version": "0.1.0",
"background_page": "background.html",
"permissions": [
"tabs", "http://*/*"
],
"browser_action": {
"name": "Play song",
"default_icon": "icon.png"
}
}有人能告诉我为什么不起作用吗?
非常感谢!!
发布于 2011-11-03 18:05:07
内容脚本无法访问父页的window对象。您需要用代码将<script>标记插入到页面中。
//bg page
chrome.tabs.executeScript(null, {
code: "injectJs('window.Grooveshark.addSongsByID([13963],true)')"
});
//content script
function injectJs(code) {
var scr = document.createElement("script");
scr.type = "text/javascript";
scr.text = code;
(document.head || document.body || document.documentElement).appendChild(scr);
}您可以通过清单将injectJs函数注入所有页面,然后在需要时调用它。
发布于 2012-02-10 09:44:17
我写了一个包装纸使这个几乎透明。脚本注入解决方案也不允许从函数中获得任何返回值,而这个代理可以。
将其包含在内容脚本中,并在GroovesharkProxy上调用方法,而不是window.Grooveshark。
https://github.com/msfeldstein/Grooveshark-Chrome-Extension-Proxy
http://www.macromeez.com/use-groovesharks-js-api-from-a-chrome-extension/
https://stackoverflow.com/questions/7999224
复制相似问题