Note that there are some explanatory texts on larger screens.

plurals
  1. POChrome Extension that loads Page Source of the active tab
    primarykey
    data
    text
    <p>I hope you can help me.</p> <p>I am trying to create an extension in Chrome which would load a source of the active tab into a variable.</p> <p>So far I have:</p> <p><strong>manifest.json</strong></p> <pre><code>{ "name": "My Extension", "manifest_version": 2, "version": "0.1", "description": "Does some simple stuff", "browser_action": { "default_icon": "logo.png" }, "background": { "scripts": ["main.js"]}} </code></pre> <p><strong>main.js</strong></p> <pre><code>chrome.browserAction.onClicked.addListener( function(tab) { var ps1 = document.getElementsByTagName('html')[0].innerHTML; window.alert(ps1); }); </code></pre> <p>but that loads the page source of the blank page. What do I need to do to get the source of the active page. </p> <p>I have done some reading and I think I need to use content script whit some listening functions, I have been searching but all answers seem to me very complicated. Would any of you be so kind to give me some easy example?</p> <p>Highly appreciate your feedback!</p> <p>Regards</p> <hr> <h2><strong>UPDATE after AdrianCooney answer:</strong></h2> <p>I changed my manifest to contain</p> <pre><code> "permissions": [ "tabs" ] </code></pre> <p>Then in main.js I did</p> <pre><code>chrome.browserAction.onClicked.addListener(function(activeTab) { chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) { var ps1=document.getElementsByTagName('html')[0].innerHTML; window.alert(ps1); }) }); </code></pre> <p>When I press the Extension button I get something like that </p> <pre><code>&lt;html&gt;&lt;/html&gt; &lt;body&gt;&lt;script src="main.js"&gt;&lt;/script&gt; &lt;/body&gt; </code></pre> <p>...no matter what tab I have active.</p> <h2>Another try with chrome.tabs.getCurrent</h2> <pre><code>chrome.browserAction.onClicked.addListener(function(activeTab) { chrome.tabs.getCurrent( function (tabs) { var ps1=document.getElementsByTagName('html')[0].innerHTML; window.alert(ps1); }) }); </code></pre> <p>The above version of main.js give exact same output as the one before, no matter what page I have active.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload