scrape

    @@ -14,9 +14,6 @@ // c.f. // http://h.hatena.ne.jp/noromanba/81805704781216737 -// omit window.webkitURL, old/minor browsers only -// https://developer.mozilla.org/en-US/docs/Web/API/URL#Browser_compatibility -// https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL#Browser_compatibility (() => { 'use strict'; @@ -34,17 +31,32 @@ .replace(PUNCT,'_').replace(/(?:_){2,}/g, '_') + '.html'; pool.download = filename; - // get <!DOCTYPE> and <html> tree, old new thing; c.f. + // get <!DOCTYPE> et al. + let root = document.documentElement; + let buff = []; + while (root.previousSibling) { + buff.unshift(root.previousSibling); + root = root.previousSibling; + } + + // dump <!DOCTYPE> et al. and <html> tree, old new thing; c.f. // https://developer.mozilla.org/en-US/docs/XMLSerializer // http://caniuse.com/#feat=xml-serializer - const snapshot = new XMLSerializer().serializeToString(document); - const snapURL = window.URL.createObjectURL(new Blob([snapshot])); + const snapshot = buff.map(node => { + return new XMLSerializer().serializeToString(node); + }).join('\n') + document.documentElement.outerHTML; + + const snapURL = window.URL.createObjectURL(new Blob([snapshot], { + // FIXME always save UTF-8 + type: document.contentType + ';charset=' + document.characterSet + })); pool.href = snapURL; pool.click(); + window.URL.revokeObjectURL(snapURL); })(); -// FYI -// if you need <html> tree w/o <!DOCTYPE>, like this; -// const snapshot = document.documentElement.outerHTML +// NOTICE +// XMLSerializer().serializeToString() sometimes too much escape TextNode e.g. +// <script> 1 && 1; 1 < 1;</script> -> <script> 1 &amp;&amp; 1 &lt; 1 </script>
  • /*
     * @title scrape
     * @description download current html snapshot
     * @include http://*
     * @include https://*
     * @license MIT License http://opensource.org/licenses/MIT
     * @javascript_url
     */
    
    // via
    // http://blog.mudatobunka.org/entry/2015/12/23/211425
    // thx id:todays_mitsui
    
    // c.f.
    // http://h.hatena.ne.jp/noromanba/81805704781216737
    
    (() => {
        'use strict';
    
        const pool = document.createElement('a');
        // POSIX class [:punct:] not impl yet, alt expanded ASCII c.f.
        // http://www.regular-expressions.info/posixbrackets.html#class
        // [!"#$%&'()*+,\-.:;<=>?@[\\\]^_`{|}~] minus "-._" plus "\s"
        const PUNCT = /[!"#$%&'()*+,:;<=>?@[\\\]^`{|}~\s]/g;
        const filename = decodeURIComponent([
            location.hostname,
            location.pathname.split('/').slice(1).join('_'),
            location.search,
            location.hash
        ].filter(s => !!s).join('_'))
        .replace(PUNCT,'_').replace(/(?:_){2,}/g, '_') + '.html';
        pool.download = filename;
    
        // get <!DOCTYPE> et al.
        let root = document.documentElement;
        let buff = [];
        while (root.previousSibling) {
            buff.unshift(root.previousSibling);
            root = root.previousSibling;
        }
    
        // dump <!DOCTYPE> et al. and <html> tree, old new thing; c.f.
        // https://developer.mozilla.org/en-US/docs/XMLSerializer
        // http://caniuse.com/#feat=xml-serializer
        const snapshot = buff.map(node => {
          return new XMLSerializer().serializeToString(node);
        }).join('\n') + document.documentElement.outerHTML;
    
        const snapURL = window.URL.createObjectURL(new Blob([snapshot], {
            // FIXME always save UTF-8
            type: document.contentType + ';charset=' + document.characterSet
        }));
        pool.href = snapURL;
    
        pool.click();
        window.URL.revokeObjectURL(snapURL);
    })();
    
    // NOTICE
    // XMLSerializer().serializeToString() sometimes too much escape TextNode e.g.
    // <script> 1 && 1; 1 < 1;</script> -> <script> 1 &amp;&amp; 1 &lt; 1 </script>
    
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/12/27 11:44:19 - 2015-12-27
  2. 2015/12/27 11:02:08 - 2015-12-27
  3. 2015/12/27 08:30:51 - 2015-12-27
  4. 2015/12/27 08:25:26 - 2015-12-27
  5. 2015/12/26 06:22:11 - 2015-12-26
  6. 2015/12/26 05:54:07 - 2015-12-26
  7. 2015/12/26 05:34:42 - 2015-12-26