简体   繁体   中英

chrome extension: sending data from popup to unlink js file

im messing around with chrome extensions. i got a litell form i did in the popup html , and im trying to do somthing with a user input. i got a file that is linked in the popup.html , that hold this function:

function click(e) {
chrome.tabs.executeScript(null, {
    file: "theJs.js"
});

}

now in the file theJs.js , i write the code i want to be executed on the current tab that is running(can get and set info on the current tab).

so my qustion is:

how do i get a info from the popup.html , and send it to theJs.js file, so i can use the user input on the current tab?

is it possible?

(sorry for my english)

use message passing

content.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.type == 'apply') {
        applySettings(request.settings);
        sendResponse();
    }
});

popup.js

chrome.tabs.getSelected(function(tab) {
    chrome.tabs.sendRequest(tab.id, { type: "apply", name: name, settings: settings },
        function(response) {
            showMessage('success', '<strong>' + name + '</strong> applied.');
        }
    );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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