Controlling pop-up transfer to content script - Google Chrome extension

PRINT SCREEN to understand my problem: link

I am creating an extension that inserts a button into a specific DOM web page. This insertion is achieved using the injectScript () function of insert.js, which is correctly executed and entered correctly in the "content_scripts" field : manifest file.

injectScript () embeds (in the DOM of the currently loaded web page, if it is of course looking for that page), a script tag containing the same inject.js file (which contains the onclick event for the button), a style tag containing css for the button , and, most importantly, a div containing the text obtained from background.html , calling chrome.extension.sendRequest ({greeting: "dami"}, function (response) {...}); the text pasted into this div is used to populate some other div from the webpage by clicking on the button that I pasted.

This works fine, but I also need to solve another problem:

The specific text that I embed on the DOM web page is only entered when chrome.tabs.onSelectionChanged , chrome.tabs.onUpdated and chrome.history.onVisited . I also want to re-insert this text whenever my popup.html changes it by changing localStorage ["builtin"] . So what I want is this: when I click the OK button on the popup.html page, I want to use chrome.extension.sendRequest ({greeting: "reintrodu"}, function (response) {...}); from popup.html to send a request for the same script content that controlled my first insertion of text, but it doesn't seem to work.

, ! . OK, popup.html .

, OK popup.html:

function closer() 
{
    if ($('input[name=check]').is(':checked'))
    {
        localStorage["builtin"] = document.getElementById("tkey_1").value;
        localStorage["build"] = "true";
    }
    else 
    {
        localStorage["builtin"] = document.getElementById("tkey_1").value;
        localStorage["build"] = "false";            
    }

    chrome.extension.sendRequest({greeting: "reintrodu"}, function(response)
    {
        alert(response.farewell);
    });         

    window.close();
}

chrome.extension.sendRequest({greeting: "reintrodu" }, function (response) {...}) script, .js

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
//      console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting === "history")
 {
    console.log("registered in history");
    sendResponse({farewell: "goodbye from history"});
    PageShowHandler();
 }   
else if (request.greeting === "historyrem")
 {
    console.log("registered in historyrem");
    sendResponse({farewell: "goodbye from historyrem"});
    PageShowHandler();
 }       
else if (request.greeting === "exploit")
 { 
    console.log("registered in exploit");
    sendResponse({farewell: "goodbye from exploit"});
    PageShowHandler();
 }
else if (request.greeting === "reintrodu") 
 { 
    console.log("registered in reintrodu");
    sendResponse({farewell: "goodbye from reintrodu"});
    //PageShowHandler();
    alert('prins reintrodu');
 }   
else if (request.greeting === "selected")
 {
    console.log("registered in selected");
    sendResponse({farewell: "goodbye from selected"});
    PageShowHandler();
 }

else
    sendResponse({}); // snub them.
});

background.html, , , , popup.html:

<html>
<head>
<script type="text/javascript">

//Fired when a URL is visited, providing the HistoryItem data for that URL.
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo)
{
    chrome.tabs.sendRequest(tabId, {greeting: "selected"}, function(response) {
        console.log(response.farewell);
    });
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 
{
    chrome.tabs.sendRequest(tab.id, {greeting: "exploit"}, function(response) {
            console.log(response.farewell);
    });
});

chrome.history.onVisited.addListener(function(result)
{
    //alert(result.url);
    //chrome.tabs.sendRequest(null, {greeting: "history"}, function(response) {
    //      console.log(response.farewell);
    //});
    chrome.windows.getCurrent(function(w) 
    {
        chrome.tabs.getSelected(w.id, function (tabul)
        {
            //alert(tabul.id);
            chrome.tabs.sendRequest(tabul.id, {greeting: "history"}, function(response) {
                    console.log(response.farewell);
            });
        });
    });     
});


chrome.history.onVisitRemoved.addListener(function(removed)
{
    //alert(result.url);
    //chrome.tabs.sendRequest(null, {greeting: "historyrem function(response) {
    //      console.log(response.farewell);
    //});
    chrome.windows.getCurrent(function(w) 
    {
        chrome.tabs.getSelected(w.id, function (tabul)
        {
            //alert(tabul.id);
            chrome.tabs.sendRequest(tabul.id, {greeting: "historyrem"}, function(response) {
                    console.log(response.farewell);
            });
        });
    });     
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{
    if(request.greeting == "dami")
    {
        if(localStorage["build"] == "true")
        {   
            sendResponse({farewell: localStorage["builtin"]});
        }
        else
        {
            sendResponse({farewell: "textul default"});
        }
    }

    else 
    {
        sendResponse({farewell: "n-am ba; du-te dracu!"});
    }
});


</script>
</head>
<body></body>
</html>

:

{
"name" : "gMail Adder ",
"version" : "1.0",
"description" : "Google Chrome Gmail Adder",
"options_page": "options.html",
"background_page": "background.html",
"run_at": "document_start",
"permissions": [
   "tabs",
   "history",
   "http://*/*",
   "https://*/*"
],
"content_scripts": [
  {
   "matches": ["*://*.google.mail.com/*", "https://*.google.mail.com/*"     ,"http://mail.google.com/*" ,"https://mail.google.com/*", "https://www.google.com/*", "http://www.google.com/*", "file:///*"],
   "css": ["toggle.css"],
   "js": ["jquery-1.4.4.min.js", "inject.js"]
  }
],
"browser_action" : {
"default_icon" : "Quest Icon 11.png",
"default_popup": "popup.html"
}
}
+3
1

, API .

, script, chrome.tabs.sendRequest.

, :

function closer() 
{
    ...

    //todo: get tabid of a tab where the content script you are interested in is

    chrome.tabs.sendRequest(tabId, {greeting: "reintrodu"}, function(response)
    {
        alert(response.farewell);

        //closing only after receiving response
        window.close();
    });         


}
+3

Source: https://habr.com/ru/post/1793915/


All Articles