How to capture data from a webpage in Chrome and display it in a Chrome extension popup?

For the Google Chrome extension, none of the Javascript that I write to control the DOM of the popup.html extension seems to affect the pop-up DOM. I can effectively manipulate the DOM of the current webpage in the browser using content_script.js, and I'm interested in capturing data from the webpage and displaying it in an extension popup window (for example: popup.html):

<div id="extensionpopupcontent">Links</div>
<a onclick="click()">Some Link</a>

<script type="text/javascript">
 function click() {
  chrome.tabs.executeScript(null, {file: "content_script.js"});
  document.getElementById("extensionpopupcontent").innerHTML = variableDefinedInContentScript;
  window.close();
 }
</script>

I tried using chrome.extension.sendRequest from the documentation at http://code.google.com/chrome/extensions/messaging.html , but I'm not sure how to use it correctly in my case, in particular the greeting and response.

contentscript.js
================
chrome.extension.sendRequest({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});
+3
1

, , . , , , chrome.extension.onRequest , script .

- ():

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (sender.tab && request.greeting == "hello")
    sendResponse({farewell: "goodbye"});
  else
    sendResponse({}); // snub them.
});

, script (sender.tab == true script).

, : http://code.google.com/chrome/extensions/messaging.html

+2

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


All Articles