繁体   English   中英

在网址chrome://中执行扩展后,chrome扩展不起作用

[英]chrome extension do not work after execute extension in url chrome://

扩展名可以正常工作,但是当我尝试在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>&nbsp;&nbsp;&nbsp;&nbsp;
        <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);

好吧,无论您设置了什么权限,扩展名都无法在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与消息(而不是队列)来回传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM