Access current tab DOM object from Chrome extension

I was looking for how to do this. I found several articles, most notably

Access to the current tab DOM object from "popup.html",

However, I am very new to JavaScript and making chrome extensions, and I am at a dead end. I assume no response has been received, which explains why it document.write("Hellp") does not work. Any help to fix this would be greatly appreciated.

I have three main files

manifest.json

{
 "name": "My First Extension",
 "version": "1.0",
 "description": "The first extension that I made.",
 "browser_action": 
 {
  "default_icon": "icon.png",
  "popup": "popup.html"
 },
 "permissions":
 [
  "tabs"
 ],
 "content_scripts": 
 [{
  "matches": ["<all_urls>"],
  "js": ["dom.js"]
 }]
}

popup.html

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

 chrome.tabs.getSelected(null, function(tab)
 {
  // Send a request to the content script.
  chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response)
  {
   document.write("Hello");
   document.write(response.title)
  });
 });

 </script>
</html>

dom.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{
 if (request.action == "getDOM")
  sendResponse({dom: document.getElementsByTagName("body")[0]});
 else
  sendResponse({}); // Send nothing..
});
+3
source share
2 answers

, , , . , , , , DOM. , , . , dom.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM")
   sendResponse({dom: document.body.innerHTML});
 else
   sendResponse({}); // Send nothing..
});
+3

, html , innerHTML. , , DOM **

DOM:

manifest.json

{
    "manifest_version": 2,
    "version" : "2.0",
    "name": "Prettify search",
    "description": "This extension shows a search result from the current page",
  "icons":{
    "128": "./img/icon128.png",
    "48": "./img/icon48.png",
    "16": "./img/icon16.png"
  },
 "page_action": {
    "default_icon": "./img/icon16.png",
    "default_popup": "popup.html",
    "default_title": "Prettify Results!"
  },

// If the context is the Mach url = sends a message to eventPage.js: active icon
  "content_scripts": [
    {
      "matches": ["http://www.whatever.cat/*"],
      "js": ["./js/content.js", "./js/jquery-3.1.1.min.js"]
    }
  ],

  "permissions": [
    "tabs",
    "http://www.whatever.cat/*",
    "http://loripsum.net/*"  //If you need to call and API here it goes
  ],

  "background":{
    "scripts": ["./js/eventPage.js"],
    "persistent": false
  }

}

Popup.js

    $(function() {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {

            chrome.tabs.sendMessage(tabs[0].id, {action: "getPage"}, function(response) {

                var importedCode = response.searchResults;
                var fakeHtml = document.createElement( 'html' );
                fakeHtml.innerHTML = importedCode; // recieved String becomes html


          });
        });

Eventpage.js

>Able/disable the extension button 
chrome.runtime.onMessage.addListener(function(req, sender, resp) {
    if(req.todo === 'showPageAction'){

        chrome.tabs.query({active:true, currentWindow:true}, function(tabs) {
            chrome.pageAction.show(tabs[0].id);
        });
    }
});

content.js

Content_Scripts API Chrome a > extension. Event_Page, js, Api

chrome.runtime.sendMessage({ todo: "showPageAction"});
window.onload = function() {

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.action == "getPage"){
        sendResponse({searchResults: document.body.innerHTML});
      }
  });  
};

popup.html

popup.js

+1

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


All Articles