2014-09-11 129 views
0

擴展工作正常,但是當我嘗試在URL「chrome:// 」(例如:chrome:// extensions /)擴展中執行時無法正常工作。在擴展程序無法在任何網址正常工作。重新加載擴展和頁面再次擴展後正常工作。 我想當我嘗試擴展在URL「chrome://」(例如:chrome:// extensions /)瀏覽器塊我的background.js。後重新加載擴展和頁面瀏覽器允許background.js, 這可能是允許部分的manifest.json鉻擴展在URL中執行擴展後無效chrome://

的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); 

回答

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,傳遞該消息來回消息而不是隊列。