0

Hi I'm working on chrome extension app for my website. The problem is I found some code that will give me what I want - the purpose for this code is to give me the source code of the current page, but I want to take source code for link

I want to define it inside the code

I didn't understand from where they get the current page to change it to the link which I want to get the source of.

This is the stack code which I found

https://stackoverflow.com/a/11696154/3587326

now what i want is any one who can convert this code from current page to page i need to define it inside the code popup.js

chrome.extension.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {
    message.innerText = request.source;
  }
});

function onWindowLoad() {

  var message = document.querySelector('#message');

  chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.extension.lastError) {
      message.innerText = 'There was an error injecting script : \n' + chrome.extension.lastError.message;
    }
  });

}

window.onload = onWindowLoad;

getPagesSource.js

function DOMtoString(document_root) {
    var html = '',
        node = document_root.firstChild;
    while (node) {
        switch (node.nodeType) {
        case Node.ELEMENT_NODE:
            html += node.outerHTML;
            break;
        case Node.TEXT_NODE:
            html += node.nodeValue;
            break;
        case Node.CDATA_SECTION_NODE:
            html += '<![CDATA[' + node.nodeValue + ']]>';
            break;
        case Node.COMMENT_NODE:
            html += '<!--' + node.nodeValue + '-->';
            break;
        case Node.DOCUMENT_TYPE_NODE:
            // (X)HTML documents are identified by public identifiers
            html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
            break;
        }
        node = node.nextSibling;
    }
    return html;
}

chrome.extension.sendMessage({
    action: "getSource",
    source: DOMtoString(document)
});
Community
  • 1
  • 1
  • Hi, welcome to SO. Please give more details - before and after or similar. Visit the [help] to see how to ask questions that will not be voted down and closed – mplungjan Jul 18 '15 at 20:16
  • i'm really really sorry of that ... I wrote wrong link and i correct it right now – Jessica Wallace Jul 18 '15 at 20:23

0 Answers0