1

extension works correctly, but when i try execute in url "chrome://" (exampe: chrome://extensions/) extension do not work correctly. And after extension do not work correctly in any url. After reload extension and page again extension works correctly. I think when i try extension in url "chrome://" (exampe: chrome://extensions/) browser block my background.js. After reloads extension and page browser allow background.js, It may be for permission section in 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);
Ramin Darvishov
  • 1,043
  • 1
  • 15
  • 30

1 Answers1

0

Well, extensions don't work on chrome:// pages, no matter what permissions you set, unless Chrome was launched with specific flags.

As to "blocking": no such thing, it's just an error in your code. When the injected script does NOT execute, you put the callback into the queue but it's not popped out. So the next time you call shift you get the wrong callback.

To fix, check for errors in script injection:

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();
    }
  }); 
};

Edit: In fact, you should not rely on the messages arriving in order, too. Assign the callbacks an ID, pass that ID with the message back and forth instead of a queue.

Xan
  • 74,770
  • 16
  • 179
  • 206