var enableRender = true;

chrome.contextMenus.create({
    "title":"Render selection",
    "contexts":["selection"], "onclick":function (info, tab) {
        var funcToInject = function () {
            var selection = window.getSelection();
            return (selection.rangeCount > 0) ? selection.toString() : '';
        };
        var jsCodeStr = ';(' + funcToInject + ')();';
        chrome.tabs.executeScript({
            code:jsCodeStr,
            allFrames:true
        }, function (selectedTextPerFrame) {
            if (chrome.runtime.lastError) {
                console.log('error:' + chrome.runtime.lastError.message);
            } else if ((selectedTextPerFrame.length > 0) && (typeof(selectedTextPerFrame[0]) === 'string')) {
                var selectedText = selectedTextPerFrame[0];
                chrome.tabs.create({
                    'url':chrome.extension.getURL("html/inject.html"),
                    'active':true
                }, function (tab) {
                    var selfTabId = tab.id;
                    chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
                        if (changeInfo.status == "complete" && tabId == selfTabId) {
                            var tabs = chrome.extension.getViews({type:"tab"});
                            // Get the latest tab opened
                            tabs[tabs.length - 1].inject(selectedText);
                        }
                    });
                });
            }
        });
    }
});

function enableDisableRender() {
    // Save the status of the extension
    chrome.storage.local.set({'ENABLE_RENDER':enableRender});

    // Update the extension icon
    var iconPath = enableRender ? "img/enabled.png" : "img/disabled.png";
    chrome.browserAction.setIcon({path:iconPath});

    // Switch enabled <> disabled
    enableRender = !enableRender;

    // Reload the page
    chrome.tabs.getSelected(null, function (tab) {
        var code = 'window.location.reload();';
        chrome.tabs.executeScript(tab.id, {code:code});
    });
}

function refreshOptions() {
    chrome.storage.local.set({
        'CUSTOM_ATTRIBUTES': localStorage['CUSTOM_ATTRIBUTES'],
        'SAFE_MODE': localStorage['SAFE_MODE'],
        'THEME': localStorage['THEME']
    });
}

chrome.browserAction.onClicked.addListener(enableDisableRender);
enableDisableRender();

@@ -17,16 +17,93 @@
  = true;
+%0Avar matchesTabUrl = chrome.runtime.getManifest().content_scripts%5B0%5D.matches;
 %0A%0Achrome
@@ -1589,16 +1589,143 @@
  %7D%0A%7D);%0A%0A
+function refreshTab(tab) %7B%0A    var code = 'window.location.reload();';%0A    chrome.tabs.executeScript(tab.id, %7Bcode: code%7D);%0A%7D%0A%0A
 function
@@ -1745,24 +1745,24 @@
 eRender() %7B%0A
-
     // Save 
@@ -2102,175 +2102,426 @@
 the 
-page%0A    chrome.tabs.getSelected(null, function (tab) %7B%0A        var code = 'window.location.reload();';%0A        chrome.tabs.executeScript(tab.id, %7Bcode:code%7D);%0A    %7D);
+active tab in the current windows that matches%0A    var tabFound = false;%0A    for (var i = 0; i %3C matchesTabUrl.length; i++) %7B%0A        var matchTabUrl = matchesTabUrl%5Bi%5D;%0A        chrome.tabs.query(%7Bactive: true, currentWindow: true, url: matchTabUrl%7D, function (tabs) %7B%0A            if (!tabFound && tabs.length %3E 0) %7B%0A                refreshTab(tabs%5B0%5D);%0A                tabFound = true;%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A%0A

Refresh the active tab only if the URL matches