0

I think I have read all the existing answers to this question on SO but could not solve my problem, so here it is:

The idea is to remove a div element by id on the current tab upon clicking on a button from a popup that is shown upon clicking on the extension icon.

Here is my code:

popup.html

 <!doctype html>
<html>
  <head>
    <title>TurboViewer</title>
    <script src="popup.js"></script>
    <script src="contentscript.js"></script>
  </head>
  <body>
    <h3>Turbo Viewer</h3>
    <button id="checkPage">Check this page now!</button>
  </body>
</html>

popup.js

document.addEventListener('DOMContentLoaded', function() {
  var checkPageButton = document.getElementById('checkPage');
  checkPageButton.addEventListener('click', function() {

    chrome.tabs.getSelected(null, function(tab) {
      // Send a request to the content script.
      chrome.tabs.sendMessage(tab.id, {action: "getDOM"}, function(response) {
        console.log("We are good");
      });
});

  }, false);
}, false);

contentscript.js

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM"){
     var sbElement = document.querySelector('div#sidebar');
     sbElement.parentElement.removeChild(sbElement);
     sendResponse({dom: "Successfully removed"});
 }

 else
   sendResponse({}); // Send nothing..
});

and finally manifest.json

{
  "manifest_version": 2,

  "name": "TurboView Plugin",
  "description": "This extension will modify your view of the webpage",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab",
   "tabs"
   ],
   "content_scripts": [
    {
        "matches": ["*://*.example.com/*"],
        "js": ["contentscript.js"]
    }
   ]
}

The first thing I notice is that my breakpoint in the contentscript.js never gets hit.

So I am not sure if it is even getting loaded.

Any ideas appreciated, Thanks

hrz
  • 313
  • 1
  • 3
  • 14
  • 2
    Rightclick the popup and `inspect` the console for errors and messages. Don't include the content script in the popup (make sure to read the extension architecture article). Use chrome.tabs.query instead of the deprecated getSelected, and chrome.runtime.onMessage instead of chrome.extension.onMessage. Make sure you've reloaded the web page so that the content script gets autoinjected or better switch to chrome.tabs.executeScript. – wOxxOm Aug 02 '17 at 03:34
  • 1
    If your user interaction *begins* with the user clicking a `browserAction` button, then the content script should be injected with [`chrome.tabs.executeScript()`](https://developer.chrome.com/extensions/tabs#method-executeScript) instead of a *manifest.json* `content_script` entry. That way your content script does not burden the browser by being injected into every page just to wait to be used. Using `chrome.tabs.executeScript()`, the script can begin functioning when it is injected with [the data, if any is needed, that has been passed to it](http://stackoverflow.com/a/40815514/3773011). – Makyen Aug 02 '17 at 05:03
  • 1
    `tabs.getSelected` is deprecated. You should use `tabs.query()`. Specifically `chrome.tabs.query({currentWindow: true, active: true}, function(tabs){});` – Makyen Aug 02 '17 at 05:08
  • Thanks guys, that helped quite a bit! – hrz Aug 02 '17 at 21:53

0 Answers0