0

I'm creating a extension for Safari that need make some changes on content of a active tab using innerHTML in specific places of this content.

But for make this, is necessary before retrieve all html content including all tags and texts.

I have a Chrome Extension, that function below works very fine! but in Safari browser don't works :-(

This works fine to Google Chrome, but not for Safari:

This works fine to Google Chrome, but not for Safari:

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;
}

So, someone know how make this same think (get full html source) on Safari across a extension?

  • Changing the `innerHTML` of the whole tab seems like a bad idea. Any Javascript variables or event bindings associated with any elements will be invalidated, because when you assign the innerHTML back it creates new elements for everything. – Barmar May 20 '16 at 01:11
  • In what way doesn't it work? – Barmar May 20 '16 at 01:12
  • @Bermar, `document.tagname.innerHTML = "html content here"` not will be applied in whole document and code above not retrieve html content. –  May 20 '16 at 01:29
  • @Bermar, you know how retrieve full html source code of active tab in Safari browser? –  May 20 '16 at 01:32
  • I think it should just be something like `document_root.innerHTML` – Barmar May 20 '16 at 01:33
  • No, in my case will be somethink like `document.getElementsByTagName("p").innerHTML`. –  May 20 '16 at 01:35
  • `getElementsByTagName` returns a `NodeList`, but `.innerHTML` is a property of each node, not the list. You have to loop over the list and get each one's `innerHTML`. – Barmar May 20 '16 at 01:38
  • You still haven't clarified your question: what is your code doing wrong in Safari? – Barmar May 20 '16 at 01:38
  • Yes, you is right! but I will change html content by tag name as showed in my last comment above. –  May 20 '16 at 01:40
  • My code above only not is able to get all html content of a tab as I want. Code above returns empty content. –  May 20 '16 at 01:43
  • Have you tried single-stepping it in the debugger to see why it's not finding anything? – Barmar May 20 '16 at 01:44
  • I only know that on Safari, the way for retrive html source code of a tab is different, but I don't know where start. This is my real trouble :-( –  May 20 '16 at 01:46
  • Do you really need to get the original **source** code, or the HTML of the current DOM (which may have been modified by Javascript)? The code above accesses the DOM, not the source code, and I don't think Javascript has access to the source. – Barmar May 20 '16 at 01:52
  • Yes, I want access the current DOM retrieved by active tab as code above does with Google Chrome. I found [this question here](http://stackoverflow.com/questions/10322864/safari-extension-retrieve-full-html-code-from-a-page), but I not understood nothing until now. –  May 20 '16 at 01:58
  • If the code isn't working, I suspect the problem is that you're not passing the correct value as `document_root`. Because otherwise the code looks like it should work. What do you see when you set a breakpoint in the function and step through it? What is `node` getting set to? – Barmar May 20 '16 at 01:59

0 Answers0