/*
* @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 et al.
let root = document.documentElement;
let buff = [];
while (root.previousSibling) {
buff.unshift(root.previousSibling);
root = root.previousSibling;
}
// dump et al. and 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.
// ->