ãã®è¨äºã®å
¬éå¾ã« noromanba æ§ãããDOMã®ããã¹ãå¤æã¯XMLSerializerã使ãã°ä¸æã§ããããï¼ãçãªãææãé ããã®ã§è¿½è¨ãã¾ããã
æ¬å½ã«ãããã¨ããããã¾ãã
ã¹ã¯ã¬ã¤ãã³ã°ãã¿ã§ãã
ã¯ãã¼ãªã³ã°+ã¹ã¯ã¬ã¤ãã³ã°ããã«ããã£ã¦ãã¹ã¯ã¼ãèªè¨¼ã®æãã£ããã¼ã¸ããæ
å ±ãåå¾ããã®ã¯ãªããªãã«åä»ãªèª²é¡ã§ãããã
ã¹ã¯ã¬ã¤ãã³ã°ã®ãã¬ã¼ã ã¯ã¼ã¯ã«ãã£ã¦ã¯èªè¨¼ã®ããã®æ©è½ãåãã£ã¦ããããã¾ããããããç¡ãå ´åã«ã¯èªåã§HTTPã®ã¬ã¹ãã³ã¹ãããã¯ãã¦Cookieé£ãããããªãã ãã¨ãã大å¤ã
ãããã100ãã¼ã¸è¶ ã®ãã¼ã¸ã対象ã«ã¹ã¯ã¬ã¤ãã³ã°ããããªãã°ããç¥ããã5, 6ãã¼ã¸ã¨ãããããã®ãã¼ã¸ããæ å ±åãããã ãã§ããã¹ã¯ã¼ãèªè¨¼ã®ããã®ä¸æºåããããªãã¦çºçãããã«ãªãã¾ãããï¼
ãããªã¨ãã«æ°ããã½ãªã¥ã¼ã·ã§ã³ããã¹ã¯ã¼ãèªè¨¼ã®çªç ´ã¯ããªãã®æã¨ããªãã®ãã©ã¦ã¶ã§ãã£ã¦ãã¾ãã¾ãããã
ããªãã®ãã©ã¦ã¶ã§ãã°ã¤ã³ãå®äºãã¦ãã¼ã¸ã表示ããã¦ãããªãã°ããã¼ã¿ã®å
¨ã¦ã¯ãã§ã«æå
ã«ããã¨ãããã¨ã§ããã²ã¨ã¾ãHTMLã¨ãã¦ä¿åãã¦ãã¾ãã°å¾ããParserã«éãã¦ã¹ã¯ã¬ã¤ãã³ã°ããã®ã¯ç°¡åãªã¯ãã§ãã
ä»åã¯ããã¾è¡¨ç¤ºãã¦ãããã¼ã¸ãHTMLã¨ãã¦ä¿åããã¹ã¯ãªãããããã¯ãã¼ã¯ã¬ããã«ãã¦ã¿ã¾ããã
ããã¯ãã¼ã¯ã¬ãã "SaveAsHTML"
ãããªããã§ä½ã£ãããã¯ãã¼ã¯ã¬ãããã³ã¬âã§ãã
ããªãã®ããã¯ãã¼ã¯ãã¼ã«ãã©ãã°&ãããããã¦ã使ããã ããã
ä¿åããããã¼ã¸ã§ããã¯ãã¼ã¯ãã¿ã³ãæ¼ãã¨ããã¤ã¢ãã°ããã¯ã¹ãåºã¦HTMLãã¡ã¤ã«ãä¿åã§ãã¾ãã
ãªããFirefoxã®å ´åãSSLã§ä¿è·ããããã¼ã¸(https: ã§å§ã¾ãã¢ãã¬ã¹ã®ãã¼ã¸)ã§ããã¯ãã¼ã¯ã¬ããã使ãã¨ãããã¯ãã¼ã¯ã¬ããã¨ã¯ããå¤é¨ã¹ã¯ãªããå®è¡ãã¦å¤§ä¸å¤«ï¼ãçãªè¦åãåºã¦ãããã¯ããããã¨ãããã¾ãã
ããã°ã£ããã¯ä»æ¹ãªãã®ã§ãChrome ãªã Opera ãªãã使ã£ã¦ãã ããã
ããã¼ã¸ã®ã½ã¼ã¹ã表示ããããã¡ãªã®ï¼
å¤ãã®å ´åã¯ããã§ãããã§ããã
ããããããã¼ã¸ã®ã½ã¼ã¹ã表示ãã§è¦ãããã½ã¼ã¹ã³ã¼ãã¯JavaScriptãå®è¡ãããåã®ãã¬ã¼ã³ãªHTMLãªãã§ãããã
ãã¨ãJavaScriptã«ãã£ã¦DOMãçµã¿æ¿ãããããã¼ã¸ã§ããçµã¿æ¿ãåã®æ§é ããè¦ãã¾ããã
ajaxã«ãã£ã¦ãã¼ãå¾ã«ã³ã³ãã³ããåã£ã¦ããã¿ã¤ãã®ãµã¤ãã§ããã°ãå¾èªã¿ã®ã³ã³ãã³ãã¯å
¨ãåå¾ã§ããªããã¨ã«ãªãã¾ãã
ããã§ãä»åã®ããã¯ãã¼ã¯ã¬ããã§ã¯ JS å®è¡å¾ã® DOM ãããã¹ãå½¢å¼ã«å¤æãã¦ä¿åãã¦ãã¾ãã
使ã£ã¦ã¿ã
使ã£ã¦ã¿ã¾ãããã
対象ã«ããã®ã¯ AngularJS ã§ããã³ãã¨ã³ããæ§ç¯ãã¦ãããã¨ã§ã馴æã¿ã® note.mu ã§ãã
ãããã®å²¸ç°ãããæ¸ããã¦ãã岸ç°æ¥è¨â
¡ã®ã¤ã³ããã¯ã¹ãã¼ã¸ãä¿åãã¾ãã
ã§ã¯ããããã¨ã
ä¿åã§ãã¾ãããã
ã¡ããã¨ã³ã³ãã³ããåãã¦ãã£ã½ãã§ãããGood.
解説
ã¹ã¯ãªããã®è§£èª¬ããã¦ã¿ã¾ãã
ã¾ãã¯ãå®ç©ã®ã¹ã¯ãªãããã
(function() { // <html> ã clone var html = document.getElementsByTagName('html')[0].cloneNode(true); // href ã src ã«æå®ãããURLã絶対ãã¹ã«å¤æ var nodes = html.querySelectorAll('[href],[src]'); for (var i=0, n=nodes.length; i<n; i++) { if (nodes[i].href) { nodes[i].href=nodes[i].href; } if (nodes[i].src) { nodes[i].src=nodes[i].src; } console.log('href: ' + nodes[i].href + ', src: ' + nodes[i].src); } // ã½ã¼ã¹ã³ã¼ããããã¹ãã§åå¾ var src = html.innerHTML; console.log(src.slice(0, 5000)); // ä¸è¨ã® src ã«ã¯ DOCTYPE ãå«ã¾ãã¦ããªãã®ã§å¥éç¨æ var name = document.doctype.name; var publicId = document.doctype.publicId; var systemID = document.doctype.systemId; var doctype = '<!DOCTYPE ' + name + (publicId ? ' PUBLIC "' + publicId + '"' : '') + (systemID ? ' "' + systemID + '"' : '') + '>'; console.log(doctype); // <html> ã¿ã°ãåæ§æ var htmlTag = '<html'; var attrs = html.attributes; for (var i=0, n=attrs.length; i<n; i++) { var attr = attrs[i]; htmlTag += ' ' + attr.nodeName + (attr.nodeValue ? '="' + attr.nodeValue + '"' : ''); } htmlTag += '>'; console.log(htmlTag); // ã½ã¼ã¹ã³ã¼ãã Blob ãªãã¸ã§ã¯ãã«å¤æãã¦URLãåå¾ var blob = new Blob([doctype, '\n', htmlTag, '\n', src, '\n</html>']); var url = window.URL || window.webkitURL; var blobURL = url.createObjectURL(blob); // <a> ãæ°ãã«ä½æãããã¦ã³ãã¼ãç¨ã®è¨å®ããããã var a = document.createElement('a'); // URI ãå ã«ãã¦ã³ãã¼ãæã®ãã¡ã¤ã«åãæ±ºå® a.download = decodeURI(location.pathname+location.hash).replace(/\//g,'__').replace(/#/g,'--') + '.html'; a.href = blobURL; a.click(); })();
æåã« var html = document.getElementsByTagName('html')[0]
㧠<html>
ã® node ãåå¾ãã¦ãã¾ãã
ãã®å¾ãhtml.innerHTML
ãåç
§ããã°ãã¼ã¸ã®ã»ã¨ãã©ã® DOM ãããã¹ãã§æã«å
¥ãã¾ãã
ãªããå ¨ã¦ã® DOMãã§ã¯ãªããã»ã¨ãã©ã® DOMããã¨ããã¨ã
<!DOCTYPE>
ã<html>
ã®å¤å´ã«ããinnerHTML
ã§ã¯ä¸çªå¤å´ã®<html>
ã¿ã°ã®å 容ãå«ãã¦ãããªã
ããã§ãã
ãã®äºã¤ã®åé¡ã解æ¶ããããã«çµæ§ã³ã¼ãéãå¢ãã¦ã¾ãããµã¼ãã
1. <!DOCTYPE> ãåæ§æ
<!DOCTYPE>
ã®æ
å ±ã¯ DocumentType ãªãã¸ã§ã¯ã ã¨ãã¦ä¿æããã¦ãããJavaScript 㧠document.doctype
ãåç
§ãããã¨ã§ã¢ã¯ã»ã¹ã§ãã¾ãã
ç¹ã«ã
document.doctype.name
document.doctype.publicId
document.doctype.systemId
ã¨ããä¸ã¤ã®å¤ãçµã¿åãããã°å
ã® <!DOCTYPE>
ãåæ§æã§ãããã§ãã
ä»åã¯ã
// ä¸è¨ã® src ã«ã¯ DOCTYPE ãå«ã¾ãã¦ããªãã®ã§å¥éç¨æ var name = document.doctype.name; var publicId = document.doctype.publicId; var systemID = document.doctype.systemId; var doctype = '<!DOCTYPE ' + name + (publicId ? ' PUBLIC "' + publicId + '"' : '') + (systemID ? ' "' + systemID + '"' : '') + '>';
ã¦ãªæãã«ãã£ã¦ã¾ãã
2. <html> ã¿ã°ãåæ§æ
innerHTML
㯠inner 㪠HTML ã®æ
å ±ãè¿ãã¦ãããã ããªã®ã§ãå
¨ä½ãå
ãã§ããã¿ã°ã«ã¤ãã¦ã®æ
å ±ãå«ãã§ãã¾ããã
æå¤ã®ã¿ã°ãå«ã㦠DOM ãããã¹ãåããããã«ãå¤å´ãããä¸ã¤ã¿ã°ã§å
ãã§ã
var targetNode = document.getElementById('target'); var tmpNode = document.createElement('div').appendChild(targettargetNode); var targetSrc = tmpNode.innerHTML;
ã®ããã«ããæ¹æ³ãããã¾ããã
ä»åã¯ãæå¤ã <html>
ã¿ã°ã ã£ãããã«ã<html>
ãããã«å¥ã®ã¿ã°ã§å
ããã¨ãã§ããªãã¨ããå¶ç´ãåãã¦ãã®æ¹æ³ã¯ä½¿ãã¾ããã§ããã
ããããªãã®ã§ã JavaScript 㧠<html>
ã®å±æ§ãèªã¿åºãã¦ã¿ã°ãåæ§æãã¾ããããããã
// <html> ã¿ã°ãåæ§æ var htmlTag = '<html'; var attrs = html.attributes; for (var i=0, n=attrs.length; i<n; i++) { var attr = attrs[i]; htmlTag += ' ' + attr.nodeName + (attr.nodeValue ? '="' + attr.nodeValue + '"' : ''); } htmlTag += '>';
ãããªæãã§ã
ã¾ãæ®éã§ããã
éãã¿ã°ã¯ </html>
ã§åºå®ãªã®ã§ãã¼ãã³ã¼ãã£ã³ã°ãã¡ããã¾ãã
3. ç¸å¯¾ãã¹ã絶対ãã¹ã«å¤æ
ããä¸ã¤ãããã¯ãã ã®ãµã¼ãã¹ç²¾ç¥ãªãã§ããã
ã½ã¼ã¹ä¸ã® href
ã src
ã«æå®ããã¦ããURLãå
¨ã¦çµ¶å¯¾ãã¹ã«å¤æãã¦ãã¾ãã
ä¿åãã HTML ããã¼ã«ã«ã§è¦ãã¨ãã«ãã¹ãåãã¦ããèããã ãã¨ããã¨ãããè¦è¶ããç²ãªè¨ããã§ããã
var nodes = html.querySelectorAll('[href],[src]'); for (var i=0, n=nodes.length; i<n; i++) { if (nodes[i].href) { nodes[i].href=nodes[i].href; } if (nodes[i].src) { nodes[i].src=nodes[i].src; } }
ãããªæãã
ç¸å¯¾ãã¹ ã ãã¡ã¤ã³ç¸å¯¾ãã¹ ã 絶対ãã¹ ã«å¤æããã«ããã£ã¦ pure JS ã§ãã¹ã®è§£æ±ºããã®é¢åããã ãªã¨æã£ã¦èºèºãããã§ãããæ®éã« element.href
ãåç
§ããã ãã§çµ¶å¯¾ãã¹ãè¿ãã¦ããã親åè¨è¨ã§ããã
å
ã® element.href
ãç¸å¯¾æå®ã§ãããã¡ã¤ã³ç¸å¯¾æå®ã§ãã絶対æå®ã§ãããelement.href
ã element.href
ã§ä¸æ¸ããã¦ããããã¨ã§çµ¶å¯¾ãã¹ã«å¤æã§ãã¾ãã
(æ¥æ¬èªçã«ä½è¨ã£ã¦ãã ããã¤ã£ã¦æãã ãã©ãäºå®ãªãã ããªã...)
4. ããã¹ãããã¡ã¤ã«ã¨ãã¦ä¿å
ã½ã¼ã¹ã³ã¼ãã®æºåãæ´ã£ããããããHTMLãã¡ã¤ã«ã¨ãã¦ä¿åãã¾ãã
File API ã®åºçªã§ããã
// ã½ã¼ã¹ã³ã¼ãã Blob ãªãã¸ã§ã¯ãã«å¤æãã¦URLãåå¾ var blob = new Blob([doctype, '\n', htmlTag, '\n', src, '\n</html>']); var url = window.URL || window.webkitURL; var blobURL = url.createObjectURL(blob); // <a> ãæ°ãã«ä½æãããã¦ã³ãã¼ãç¨ã®è¨å®ããããã var a = document.createElement('a'); // URI ãå ã«ãã¦ã³ãã¼ãæã®ãã¡ã¤ã«åãæ±ºå® a.download = decodeURI(location.pathname+location.hash).replace(/\//g,'__').replace(/#/g,'--') + '.html'; a.href = blobURL; a.click();
ã¾ã new Blob()
㧠String
ã Blob
ãªãã¸ã§ã¯ãã«å¤æãã¾ãã
window.URL.createObjectURL()
㧠Blob
ãåç
§ããURLãåå¾ãã¦ã<a>
ã¿ã°ã® href
ã«è¨å®ãã¾ãã
<a>
ã¿ã°ã® download
å±æ§ã«ãã¡ã¤ã«åãæå®ãããã¨ã§ã<a>
ã¿ã°ãã¯ãªãã¯ããéã®åä½ãç»é¢é·ç§»ãããã¡ã¤ã«ã¨ãã¦ãã¦ã³ãã¼ãã«å¤æ´ããã¾ãã
æå¾ã«ãa.click()
ãã¦ãããã°ãã¤ã¢ãã°ããã¯ã¹ãéãã¦ã½ã¼ã¹ã³ã¼ããä¿åã§ããããã«ãªãã¾ãã
ã¯ã³ã©ã¤ãã¼å
ãã¦ãã¹ã¯ãªããã®å 容ã®è§£èª¬ã¯ãããªããã«ãã¦ã
ããã¯ãã¼ã¯ã¬ããã¨ãã¦ä½¿ãããã«ã¯æ¹è¡ãåãé¤ãã¦1è¡ã«ãã¦ãããªããã°ããã¾ããã
ããã¨ãã¹ã¯ãªããå
¨ä½ããªãã¹ãçãæ¹ãããã®ã§é©åº¦ã« Golf ãã¾ãããã
ã¾ã㯠Golf ããã
ã°ãã¼ãã«ãæ±ããã«èå¥åvar
ãåãé¤ãããã«ãå¤æ°ã¯å
¨ä½ãã©ãããã¦ããç¡åé¢æ°ã®ä»®å¼æ°ã«æ¼ãè¾¼ãã§ãã¾ãã¾ãã
(function(window, document, location, doctype, nodes, html, src, htmlTag, attrs, a, i, n, t) { html = document.getElementsByTagName('html')[0].cloneNode(true); nodes = html.querySelectorAll('[href],[src]'); for (i=0, n=nodes.length; i<n; i++) { t = nodes[i]; if (t.href) { t.href = t.href; } if (t.src) { t.src = t.src; } ;;; console.log('href: ' + t.href + ', src: ' + t.src); } src = html.innerHTML; ;;; console.log(src.slice(0, 5000)); doctype = document.doctype; doctype = '<!DOCTYPE ' + doctype.name + (doctype.publicId ? ' PUBLIC "' + doctype.publicId + '"' : '') + (doctype.systemID ? ' "' + doctype.systemID + '"' : '') + '>'; ;;; console.log(doctype); htmlTag = '<html'; attrs = html.attributes; for (i=0, n=attrs.length; i<n; i++) { t = attrs[i]; htmlTag += ' ' + t.nodeName + (t.nodeValue ? '="' + t.nodeValue + '"' : ''); } htmlTag += '>'; ;;; console.log(htmlTag); a = document.createElement('a'); a.download = decodeURI(location.pathname+location.hash).replace(/\//g,'__').replace(/#/g,'--') + '.html'; a.href = (window.URL || window.webkitURL).createObjectURL( new Blob([doctype, '\n', htmlTag, '\n', src, '\n</html>']) ); a.click(); })(window, document, location);
Golf ããã¨ãè¨ãã¤ã¤å¤æ°åãé·ãã¾ã¾ã§ãããããã¯å¾ã§ /packer/ ãçãå¤æ°åã«ç½®ãæãã¦ãããã®ã§ããã®æç¹ã§ã¯å¯èªæ§ãæ®ãã¦ãã¾ãã
console.log()
ã¨ãã®ãããã°ç¨ã®è¡ã« ;;;
ãã¤ãã¦ããã®ãå¾ã§ /packer/ ã«åé¤ãã¦ãããããã®ç®å°ã§ãã
ã¨ããããã§ã /packer/ ã§å§ç¸®ãã¾ãã
Shrink variables ã«âãå
¥ãã¦...
(function(b,c,d,e,f,g,h,j,k,a,i,n,t){g=c.getElementsByTagName('html')[0].cloneNode(true);f=g.querySelectorAll('[href],[src]');for(i=0,n=f.length;i<n;i++){t=f[i];if(t.href){t.href=t.href}if(t.src){t.src=t.src}}h=g.innerHTML;e=c.doctype;e='<!DOCTYPE '+e.name+(e.publicId?' PUBLIC "'+e.publicId+'"':'')+(e.systemID?' "'+e.systemID+'"':'')+'>';j='<html';k=g.attributes;for(i=0,n=k.length;i<n;i++){t=k[i];j+=' '+t.nodeName+(t.nodeValue?'="'+t.nodeValue+'"':'')}j+='>';a=c.createElement('a');a.download=decodeURI(d.pathname+d.hash).replace(/\//g,'__').replace(/#/g,'--')+'.html';a.href=(b.URL||b.webkitURL).createObjectURL(new Blob([e,'\n',j,'\n',h,'\n</html>']));a.click()})(window,document,location);
ã¯ãã
ããã¯ãã¼ã¯ã¬ããå
å
ã»ã©ã®1è¡ã¹ã¯ãªããã®å
é ã« javascript:
ãä»å ããã ãã§ããã
HTMLä¸ã«åãè¾¼ãå ´åã¯ããã«ã¯ã©ã¼ã"
ãåç
§æåã®"
ã«ç½®ãæããã®ãå¿ããã«ã
ã¾ã¨ã
ãã®ããã¯ãã¼ã¯ã¬ãããåã
ãã欲ããã£ããã§ããããã¨ãã£ã¦ãã£ãã£ãã¨æ¸ãã®ããªããªãã«é¢åã ã£ããã§ãããã
ããããã¡ããã¨å½¢ã«ããããã«ã¯å¤§äºã«ä½¿ã£ã¦ããããã§ãã
JSå®è¡å¾ã®ã½ã¼ã¹ãä¿åããããªãäºãåå¹´ã«ä¸åãããããç¡ããã©ã
追è¨
ãã®è¨äºã®å
¬éå¾ã« noromanba æ§ãããDOMã®ããã¹ãå¤æã¯XMLSerializerã使ãã°ä¸æã§ããããï¼ãçãªãææãé ãã¾ããã
æ¬å½ã«ãããã¨ããããã¾ãã
// ä¸æï¼ï¼ var snapshot = new XMLSerializer().serializeToString(document);
ããã§ãã顧客ãæ¬å½ã«å¿ è¦ã ã£ããã®ã¯ã
åè
以ä¸ãåèã«ãã¤ã¤æ¸ããã¦ãããã¾ãããæè¬ï¼