Manifesto:

{ "manifest_version": 2, "name": "Test", "description": "Тест расширения", "version": "1.1", "icons": { "128": "128.png" }, "browser_action": { "default_popup": "index.html" }, "permissions": [ "activeTab" ], "background": { "scripts": [ "jquery.js", "background.js" ] }, "content_scripts": [ { "matches": ["http://*/*"], "css": ["style.css"], "js": ["jquery.js","popup.js"] } ] } 

Background.js

 chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension"); if (request.greeting == "hello") sendResponse({farewell: "goodbye"}); }); 

Popup.js

 chrome.runtime.sendMessage({greeting: "hello"}, function(response) { console.log(response.farewell); }); 

If you go to some site with http: // then there will be goodbye in the console, if I understood correctly, then we roughly send the hello message to the extension, there is a check and the answer is sent, "goodbye"

How to get data from the page to the extension?

Example: take some block from Yandex with id="data" (for example), get the contents var b = $('#data').text(); and pass to your extension and, for example, insert into your markup, approximately into a block with id="test" .... $('#test').text(b);

Can you voluntarily direct you to the right path? To give a small example in general would be a miracle =) Thank you.

    1 answer 1

    To work with the data on the page you need a content-script . Strictly speaking, you already have it (specified in the corresponding section of the manifest), one script is called popup.js .
    This suggests that there is some confusion. In the terminology of extensions: popup and content-script are different things.

    Popup is a popup window in the chromium expansion area.

    Content-script is a script that works directly on the pages (corresponding to the matches mask in the manifest).

    In general, you have the correct code for the content script, but I recommend sticking to the established practice of naming modules.

    Regarding the issue of obtaining data, you can refer to the elements using standard DOM methods. Now everything can be done without the need to connect jQeury.

    content-script.js

     document.addEventListener('DOMContentLoaded', () => { // Берем нужный эелемент и получаем его текст const el = document.querySelector('#data'); const elText = el.innerText; // Отправляем в background chrome.runtime.sendMessage({ text: elText }); }); 

    background.js

     // Слушаем сообщения от CS chrome.runtime.onMessage.addListener(function (request) { console.log(request.text); });