扩展程序正常工作,但是当我尝试在url中执行“chrome:// ”(例如:chrome:// extensions /)扩展程序无法正常工作时。并且在扩展后无法在任何URL中正常工作。重新加载扩展和页面再次扩展工作正常。 我想当我在网址“chrome:// ”(例如:chrome:// extensions /)中尝试扩展时,浏览器阻止了我的background.js。重新加载扩展后,页面浏览器允许background.js, 它可能是manifest.json
中的权限部分的manifest.json
{
"manifest_version": 2,
"name": "X Plugin",
"description": "Post selected text for translate",
"version": "0.1",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"icons": {
"64": "icon_64x64.png"
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"http://*/",
"https://*/"
]
}
popup.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="popup.js"></script>
</head>
<body>
<form id="addbookmark" method="POST" action="http://www.somesite.az" target="_blank">
<img src="logo.png" />
<p><label for="direction">Direction</label><br />
<select name="lang_left" id="id_lang_left" class="dlc_select"></select>
<select name="lang_right" id="id_lang_right" class="dlc_select"></select>
<p><label for="text">Text</label><br />
<textarea name="from" id="id_from" rows="6" cols="35"></textarea>
</p>
<p>
<input id="translate" type="submit" name="translate" value="Translate" />
<span id="status-display"></span>
</p>
</form>
</body>
</html>
popup.js
// This callback function is called when the content script has been
// injected and returned its results
function onPageInfo(o) {
document.getElementById('id_from').innerText = o.summary;
}
window.addEventListener('load', function(evt) {
chrome.extension.getBackgroundPage().getPageInfo(onPageInfo);
});
background.js
// Array to hold callback functions
var callbacks = [];
// This function is called onload in the popup code
function getPageInfo(callback) {
// Add the callback to the queue
callbacks.push(callback);
// Inject the content script into the current page
chrome.tabs.executeScript(null, { file: 'content_script.js' });
};
// Perform the callback when a request is received from the content script
chrome.extension.onMessage.addListener(function(request) {
// Get the first callback in the callbacks array
// and remove it from the array
var callback = callbacks.shift();
// Call the callback function
callback(request);
});
content_script.js
// This script is only injected when the popup form is loaded
// (see popup.js), so we don't need to worry about waiting for page load
// Object to hold information about the current page
var pageInfo = {
'title': document.title,
'url': window.location.href,
'summary': window.getSelection().toString()
};
// Send the information back to the extension
chrome.extension.sendMessage(pageInfo);
答案 0 :(得分:1)
好吧,无论您设置了什么权限,扩展程序都无法在chrome://
页面上使用,除非Chrome是使用特定标记启动的。
关于“阻止”:没有这样的事情,这只是代码中的错误。当注入的脚本不执行时,您将回调放入队列但不会弹出。因此,下次您致电shift
时,您会收到错误的回调。
要修复,请检查脚本注入中的错误:
function getPageInfo(callback) {
// Add the callback to the queue
callbacks.push(callback);
// Inject the content script into the current page
chrome.tabs.executeScript(null, { file: 'content_script.js' }, function(){
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
callbacks.pop();
}
});
};
编辑:事实上,你也不应该依赖顺序到达的消息。为回调分配一个ID,使用该消息来回传递该ID,而不是队列。