Task: get the contents of the html-file and paste it into the page.
My logic is: as soon as the necessary page is loaded, content.js sends a message to background.js , which in turn takes the $.get("_add.html",function(data){} file $.get("_add.html",function(data){} and sends the information back to content.js
https://developer.chrome.com/extensions/messaging#simple - looked here. It seems that the first option is the most suitable. Works. Then he began to connect his file, from which it is necessary to obtain information. The sending itself does not work out
Can you please tell me how to do it ???
All files - https://github.com/bart96-b/sendMessage
manifest.json
{ "manifest_version": 2, "name": "name", "version": "1.0.0.0", "description": "description", "permissions": [ "tabs", "http://*/*", "https://*/*" ], "background": { "page": "background.html" }, "content_scripts": [{ "matches": [ "https://www.google.ru/*" ], "js": [ "content.js" ] }] } background.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="jquery-3.0.0.min.js"></script> <script type="text/javascript" src="background.js"></script> </head> <body></body> </html> background.js
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if (request.greeting == "hello"){ $.get("add.html",function(data){ sendResponse(data); // Не передается (1) sendResponse("123"); // Не передается (2) }) // sendResponse(data); // Не передается (3) // sendResponse("456"); // Передается (4) } } ); content.js
chrome.runtime.sendMessage({greeting: "hello"}, function(response) { console.log(response); });