Skip to content

Instantly share code, notes, and snippets.

@shogun70
Last active December 16, 2015 02:29
Show Gist options
  • Select an option

  • Save shogun70/5362893 to your computer and use it in GitHub Desktop.

Select an option

Save shogun70/5362893 to your computer and use it in GitHub Desktop.

Revisions

  1. shogun70 revised this gist Apr 30, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -107,9 +107,9 @@
    function (){
    // WARN: The loader is enabled for
    // - WebKit based browsers
    // - IE 9+
    // - IE 10+
    // - FireFox 4+
    // - Opera 11+
    // - Opera 13+
    // - 3DS
    match = /webkit|(firefox)[\/\s](\d+)|(opera)[\s\S]*version[\/\s](\d+)|(msie)\s(\d+)|3ds/i.exec(navigator.userAgent);

    @@ -127,7 +127,7 @@
    }

    // match[5] == IE
    if (match[5] && +match[6] < 9) {
    if (match[5] && +match[6] < 10) {
    return false;
    }

  2. shogun70 revised this gist Apr 30, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -122,7 +122,7 @@
    return false;
    }
    // match[3] == Opera
    if (match[3] && +match[4] < 11) {
    if (match[3] && +match[4] < 13) {
    return false;
    }

  3. shogun70 revised this gist Apr 30, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    },

    setHTML: function(html) {
    document.open();
    document.open('text/html', 'replace'); // replace current history entry (on IE and Firefox)
    document.write(html);
    document.close();
    },
  4. shogun70 revised this gist Apr 20, 2013. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,24 @@
    (function(window, document, detector, timeout, scripts) {

    var bootScript = $('script'); // bootScript is used in loadScript()
    if (!('onload' in bootScript) || !detector()) {
    alert('Browser not supported. No action');
    bootScript.setAttribute('onload', ';'); // old Safari will have a misleading ('onload' in bootScript)
    if (typeof bootScript.onload !== 'function' || !detector()) {
    alert('Browser not supported. No action'); // NOTE this alert pops-up twice on IE <= 8 - parsing restarts when charset is declared
    return;
    }

    // Prevent the browser from downloading website resources
    document.write('<plaintext style="display:none">');

    // read and write of document markup MUST use docProxy.
    var docProxy = window.docProxy = { // NOTE: rewriting the page deletes window.docProxy
    var docProxy = window.docProxy = { // NOTE: rewriting the page deletes window.docProxy (usually!?)

    state: 'loading', // `state` transitions to 'loaded' at window.onload

    getHTML: function() {
    var plaintext = $('plaintext');
    if (!plaintext) throw 'Not captured'; // NOTE this shouldn't be necessary.
    var html = plaintext.textContent || plaintext.innerText;
    if (!plaintext) throw 'Not captured'; // NOTE this shouldn't be necessary.
    var html = plaintext.textContent || plaintext.innerText || plaintext.firstChild.nodeValue;
    return html;
    },

    @@ -32,6 +33,7 @@
    restore: function() {
    var html = docProxy.getHTML();
    docProxy.setHTML(html);
    delete window.docProxy; // WARN rewriting page should delete this anyway but doesn't seem to happen on old Safari
    }

    }
    @@ -45,7 +47,7 @@
    FIXME: what if this occurs before `window.onload` or `scripts.onerror`
    */
    delay(function() {
    if (!docProxy) return; // docProxy undefined means already rewritten
    if (!window.docProxy) return; // docProxy undefined means already rewritten
    alert('Timed out waiting for document rewrite. Restoring page.');
    docProxy.restore();
    }, timeout);
    @@ -140,6 +142,7 @@
    <!DOCTYPE html>
    <html lang="en" id="html">
    <head id="head">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>plaintext recovery test</title>
    <style>
    section { display: block; border: 1px solid red; }
  5. shogun70 revised this gist Apr 14, 2013. 1 changed file with 57 additions and 35 deletions.
    92 changes: 57 additions & 35 deletions gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -3,57 +3,79 @@
    (function(window, document, detector, timeout, scripts) {

    var bootScript = $('script'); // bootScript is used in loadScript()
    if (!('onload' in bootScript)) {
    alert('script.onload not supported. No action');
    return;
    }

    // If the detector fails, exit
    if (!detector()) {
    alert('detector() failed. No action');
    if (!('onload' in bootScript) || !detector()) {
    alert('Browser not supported. No action');
    return;
    }

    // Prevent the browser from downloading website resources
    document.write('<plaintext style="display:none">');

    var failsafeTimer = delay(function() {
    if (!$('plaintext')) return;
    requestRewrite();
    alert('Timed out waiting for document rewrite. Rewriting page.');
    }, timeout);
    // read and write of document markup MUST use docProxy.
    var docProxy = window.docProxy = { // NOTE: rewriting the page deletes window.docProxy

    queue(scripts,
    function() { requestRewrite(); alert('Scripts successfully loaded. Rewriting anyway.'); }, // oncomplete
    function() { requestRewrite(); alert('Scripts failed. Rewriting page.'); } // onerror
    );
    state: 'loading', // `state` transitions to 'loaded' at window.onload

    var pageLoaded = false;
    window.onload = function() {
    pageLoaded = true;
    if (rewriteRequested) delay(rewrite);
    }

    var rewriteRequested = false;
    function requestRewrite() {
    rewriteRequested = true;
    window.clearTimeout(failsafeTimer);
    if (pageLoaded) delay(rewrite);
    }

    function rewrite() {
    if (!rewriteRequested) return;
    rewriteRequested = false;
    getHTML: function() {
    var plaintext = $('plaintext');
    var html = plaintext.textContent || plaintext.innerText;
    if (!plaintext) throw 'Not captured'; // NOTE this shouldn't be necessary.
    var html = plaintext.textContent || plaintext.innerText;
    return html;
    },

    setHTML: function(html) {
    document.open();
    document.write(html);
    document.close();
    },

    restore: function() {
    var html = docProxy.getHTML();
    docProxy.setHTML(html);
    }

    }

    /*
    Dead-man's page restore
    If scripts fail to load, or
    if there is an unhandled error that prevents scripts from rewriting the page,
    this timeout function will restore the original HTML of the page,
    that is, without the `<plaintext>` tag and any preceding markup.
    FIXME: what if this occurs before `window.onload` or `scripts.onerror`
    */
    delay(function() {
    if (!docProxy) return; // docProxy undefined means already rewritten
    alert('Timed out waiting for document rewrite. Restoring page.');
    docProxy.restore();
    }, timeout);


    var restoring = false; // `restoring` is set by queue(scripts).onerror
    window.onload = function() {
    docProxy.state = 'loaded';
    if (restoring) {
    docProxy.restore();
    return;
    }
    if (docProxy.onload) delay(docProxy.onload);
    }


    queue(scripts,
    function() { // oncomplete
    alert('Scripts successfully loaded.');
    },
    function() { // onerror
    alert('Scripts failed. Restoring page.');
    restoring = true;
    if (docProxy.state == 'loaded') docProxy.restore();
    }
    );

    function queue(srcList, oncomplete, onerror) {
    var list = srcList.slice(0);
    window.setTimeout(queueback);
    delay(queueback);

    function queueback() {
    if (!list.length) {
  6. shogun70 created this gist Apr 11, 2013.
    140 changes: 140 additions & 0 deletions gistfile1.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,140 @@
    <!DOCTYPE html>
    <script>
    (function(window, document, detector, timeout, scripts) {

    var bootScript = $('script'); // bootScript is used in loadScript()
    if (!('onload' in bootScript)) {
    alert('script.onload not supported. No action');
    return;
    }

    // If the detector fails, exit
    if (!detector()) {
    alert('detector() failed. No action');
    return;
    }

    // Prevent the browser from downloading website resources
    document.write('<plaintext style="display:none">');

    var failsafeTimer = delay(function() {
    if (!$('plaintext')) return;
    requestRewrite();
    alert('Timed out waiting for document rewrite. Rewriting page.');
    }, timeout);

    queue(scripts,
    function() { requestRewrite(); alert('Scripts successfully loaded. Rewriting anyway.'); }, // oncomplete
    function() { requestRewrite(); alert('Scripts failed. Rewriting page.'); } // onerror
    );

    var pageLoaded = false;
    window.onload = function() {
    pageLoaded = true;
    if (rewriteRequested) delay(rewrite);
    }

    var rewriteRequested = false;
    function requestRewrite() {
    rewriteRequested = true;
    window.clearTimeout(failsafeTimer);
    if (pageLoaded) delay(rewrite);
    }

    function rewrite() {
    if (!rewriteRequested) return;
    rewriteRequested = false;
    var plaintext = $('plaintext');
    var html = plaintext.textContent || plaintext.innerText;
    document.open();
    document.write(html);
    document.close();
    }

    function queue(srcList, oncomplete, onerror) {
    var list = srcList.slice(0);
    window.setTimeout(queueback);

    function queueback() {
    if (!list.length) {
    oncomplete();
    return;
    }
    var src = list.shift();
    loadScript(src, queueback, onerror);
    }
    }

    function loadScript(url, onload, onerror) {
    var script = document.createElement('script');
    script.onload = onload;
    script.onerror = onerror;
    script.src = url;
    bootScript.parentNode.insertBefore(script, bootScript);
    return script;
    }

    function delay(fn, duration) { return window.setTimeout(fn, duration); }

    function $(tag) { return document.getElementsByTagName(tag)[0]; }

    })( window, document,
    // detector function
    function (){
    // WARN: The loader is enabled for
    // - WebKit based browsers
    // - IE 9+
    // - FireFox 4+
    // - Opera 11+
    // - 3DS
    match = /webkit|(firefox)[\/\s](\d+)|(opera)[\s\S]*version[\/\s](\d+)|(msie)\s(\d+)|3ds/i.exec(navigator.userAgent);

    if (!match) {
    return false;
    }

    // match[1] == Firefox
    if (match[1] && +match[2] < 4) {
    return false;
    }
    // match[3] == Opera
    if (match[3] && +match[4] < 11) {
    return false;
    }

    // match[5] == IE
    if (match[5] && +match[6] < 9) {
    return false;
    }

    return true;
    },
    3000, // Failsafe timeout
    [ // NOTE: any number of startup scripts
    'notfound.js'
    ]
    );
    </script>
    <!DOCTYPE html>
    <html lang="en" id="html">
    <head id="head">
    <title>plaintext recovery test</title>
    <style>
    section { display: block; border: 1px solid red; }
    section p { border: 1px solid green; }
    </style>
    </head>
    <body id="body">
    <div>
    <h1>plaintext recovery test</h1>
    <section>
    Narrative3
    <p>Paragraph</p>
    <ul>
    <li>One</li>
    <li>Two</li>
    </ul>
    </section>
    </div>
    </body>
    </html>