谷歌浏览器扩展程序不起作用
它是一个简单的扩展,调用 Xmlhttprequest 将 POST 数据发送到表单。我还在 js 代码的开头/结尾添加了简单的消息框...代码是从后台页面调用的,并且已在 manifest.json 中授予了正确的权限。 但是,当我单击此扩展的按钮时,什么也没有发生。
下面给出的是扩展的js代码 -
alert("Beginning of code block");
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","http://taurusarticlesubmitter.appspot.com/sampleform",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=Arvind&description=Test description&[email protected]");
alert("End of code block");
另外,我将以下代码添加到background.html -
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {file: "cdr.js"});
});
chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
</script>
最后,下面给出的是我的manifest.json -
{
"name": "My Second Extension",
"version": "1.0",
"background_page": "background.html",
"description": "The second extension that I made.",
"browser_action": {
"name": "Data in iframe",
"default_icon": "icon.png"
},
"permissions": [ "tabs",
"bookmarks",
"http://*/*",
"https://*/*",
"unlimitedStorage"
]
}
It is a simple extension that invokes Xmlhttprequest to send POST data to a form. I have also added simple message boxes at beginning/end of the js code... The code is being invoked from a background page and correct permissions have been granted in manifest.json.
However when I click on the button for this extension, nothing is happening.
Given below is the js code for the extension-
alert("Beginning of code block");
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","http://taurusarticlesubmitter.appspot.com/sampleform",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=Arvind&description=Test description&[email protected]");
alert("End of code block");
Also, I added the following code to background.html--
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {file: "cdr.js"});
});
chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
</script>
Finally, given below is my manifest.json--
{
"name": "My Second Extension",
"version": "1.0",
"background_page": "background.html",
"description": "The second extension that I made.",
"browser_action": {
"name": "Data in iframe",
"default_icon": "icon.png"
},
"permissions": [ "tabs",
"bookmarks",
"http://*/*",
"https://*/*",
"unlimitedStorage"
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的第一个代码块是 cdr.js ?那么您就不从后台页面运行它。相反,您的后台页面会加载一个尝试发送请求的内容脚本。内容脚本以它们所注入的页面的权限运行。因此,如果该页面没有向 taurusarticlesubmitter.appspot.com 发送请求的权限,那么内容脚本也不会具有必要的权限。
如果您的内容脚本需要执行特权操作(例如向第三方页面发送请求),则应 向后台页面发送消息,后台页面就得这么做。
I assume that your first code block is
cdr.js
? Then you are not running it from the background page. Instead your background page loads a content script that tries to send a request. Content scripts run with the privileges of the page that they have been injected into. So if that page doesn't have privileges to send a request to taurusarticlesubmitter.appspot.com then the content script won't have the necessary privileges either.If your content script needs to perform a privileged action (like sending a request to a third-party page) it should send a message to the background page and the background page will have to do it.