0

When developing a Chrome Extension, how do you access to the source code for the current tab?

The current solution I have implemented is doing a request, so I get the source code from the request object. But as I do this, I need to give permission to every site in the manifest.json file. I don't want to use wildcards to give permission to all pages, but I don't want to add only specific URLs, so I would like to find a more straight-forward solution that doesn't include the fact of making a request.

The current solution below:

    function getSourceCode(url) {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          var parser = new DOMParser();
          source_code = parser.parseFromString(xhr.responseText, "text/html");    
          // handle source code ...
        }
      };
      xhr.send();
    }

Thanks!

Emily
  • 316
  • 2
  • 9
  • I did my research before asking the question and couldn't find a direct solution, I found the one I posted so far. – Emily Dec 04 '15 at 15:09
  • [Chrome Extension that loads Page Source of the active tab](http://stackoverflow.com/a/18580962) – wOxxOm Dec 04 '15 at 15:11
  • I need to access the source code of the page to find a specific tag, and this is only necessary when the user clicks on the Chrome Extension. – Emily Dec 04 '15 at 15:24
  • Use the [`activeTab`](https://developer.chrome.com/extensions/activeTab) permission + https://stackoverflow.com/questions/11261715/chrome-extension-get-specific-part-of-the-current-tab-page-in-dom-object-and-di/11272931#11272931 – Rob W Dec 04 '15 at 15:24
  • @wOxxOm [`chrome.tabs.executeScript`](https://developer.chrome.com/extensions/tabs#method-executeScript) as shown in your original duplicate if the most reasonable method, the key from my comment is the [activeTab](https://developer.chrome.com/extensions/activeTab) permission, which removes the need for any host permissions. – Rob W Dec 04 '15 at 15:30
  • On the other hand maybe it's time to give a new canonic up-to-date answer that may be used as a reference... – wOxxOm Dec 04 '15 at 15:32
  • Yes, none of the links I have seen are even marked as a solution and with very few points. I thought the same as you, it is a normal problem and someone may have solved it already. – Emily Dec 04 '15 at 15:34
  • Could use chrome.tabs.executeScript instead but I still have to add the links to the manifest file. – Emily Dec 04 '15 at 17:14

0 Answers0