Create sadbooks.js
Browse files- sadbooks.js +121 -0
sadbooks.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
(function() {
|
| 2 |
+
'use strict';
|
| 3 |
+
function applyStyles(elem,obj) {
|
| 4 |
+
for (var prop in obj) elem.style[prop]=obj[prop];
|
| 5 |
+
}
|
| 6 |
+
var frame=document.createElement("iframe");
|
| 7 |
+
frame.src=document.currentScript.src.slice(0,document.currentScript.src.lastIndexOf('.js')+1)+'html';
|
| 8 |
+
applyStyles(frame,{
|
| 9 |
+
zIndex:2147483647,
|
| 10 |
+
border:"none",
|
| 11 |
+
position:"fixed",
|
| 12 |
+
bottom:0,
|
| 13 |
+
left:0,
|
| 14 |
+
width:"100vw",
|
| 15 |
+
height:"200px"
|
| 16 |
+
});
|
| 17 |
+
document.body.appendChild(frame);
|
| 18 |
+
function getDOM(startingFrom) {
|
| 19 |
+
var stack=[startingFrom],result=[],i=0,parents=[],childIndices=[0];
|
| 20 |
+
while (stack.length) {
|
| 21 |
+
if (stack[0]==="CLOSE") {
|
| 22 |
+
parents.splice(-1,1);
|
| 23 |
+
childIndices.splice(-1,1);
|
| 24 |
+
} else if (stack[0].nodeType===Node.DOCUMENT_NODE) {
|
| 25 |
+
if (stack[0].childNodes)
|
| 26 |
+
for (var i=0;i<stack[0].childNodes.length;i++)
|
| 27 |
+
stack.splice(i+1,0,stack[0].childNodes[i]);
|
| 28 |
+
} else {
|
| 29 |
+
var nodeData={
|
| 30 |
+
children:[],
|
| 31 |
+
parent:parents[parents.length-1]
|
| 32 |
+
};
|
| 33 |
+
switch (stack[0].nodeType) {
|
| 34 |
+
case Node.ELEMENT_NODE:
|
| 35 |
+
nodeData.type='element';
|
| 36 |
+
nodeData.tagName=stack[0].tagName.toLowerCase();
|
| 37 |
+
nodeData.attributes={};
|
| 38 |
+
nodeData.path='';
|
| 39 |
+
nodeData.html=stack[0].innerHTML;
|
| 40 |
+
nodeData.text=stack[0].textContent;
|
| 41 |
+
childIndices[childIndices.length-1]++;
|
| 42 |
+
for (var elem of parents) nodeData.path+=result[elem].tagName.toLowerCase()+'>';
|
| 43 |
+
nodeData.path+=nodeData.tagName+`:nth-child(${childIndices[childIndices.length-1]})`;
|
| 44 |
+
for (var attr of stack[0].attributes) nodeData.attributes[attr.name]=attr.value;
|
| 45 |
+
if (parents.length) result[nodeData.parent].children.push(result.length);
|
| 46 |
+
break;
|
| 47 |
+
case Node.TEXT_NODE:
|
| 48 |
+
case Node.COMMENT_NODE:
|
| 49 |
+
nodeData.type=stack[0].nodeType===Node.TEXT_NODE?'text':'comment';
|
| 50 |
+
nodeData.text=stack[0].data;
|
| 51 |
+
if (parents.length) result[nodeData.parent].children.push(result.length);
|
| 52 |
+
break;
|
| 53 |
+
case Node.DOCUMENT_TYPE_NODE:
|
| 54 |
+
nodeData.type='doctype';
|
| 55 |
+
nodeData.text=stack[0].name;
|
| 56 |
+
break;
|
| 57 |
+
}
|
| 58 |
+
if (stack[0].childNodes) {
|
| 59 |
+
for (var i=0;i<stack[0].childNodes.length;i++)
|
| 60 |
+
stack.splice(i+1,0,stack[0].childNodes[i]);
|
| 61 |
+
parents.push(result.length);
|
| 62 |
+
childIndices.push(0);
|
| 63 |
+
stack.splice(i+1,0,"CLOSE");
|
| 64 |
+
}
|
| 65 |
+
result.push(nodeData);
|
| 66 |
+
}
|
| 67 |
+
stack.splice(0,1);
|
| 68 |
+
}
|
| 69 |
+
result.type="DOM";
|
| 70 |
+
return result;
|
| 71 |
+
}
|
| 72 |
+
frame.onload=e=>{
|
| 73 |
+
frame.contentWindow.postMessage(getDOM(document),"*");
|
| 74 |
+
};
|
| 75 |
+
function whenIreceive(e) {
|
| 76 |
+
if (e.data.type) switch (e.data.type) {
|
| 77 |
+
case "COMPUTED STYLES":
|
| 78 |
+
var elem=document.querySelector(e.data.path),styles={},computed=window.getComputedStyle(elem);
|
| 79 |
+
if (e.data.set) {
|
| 80 |
+
elem.style[e.data.set.prop]=e.data.set.val;
|
| 81 |
+
}
|
| 82 |
+
for (var property of computed) styles[property]=computed[property];
|
| 83 |
+
frame.contentWindow.postMessage({
|
| 84 |
+
type:"RE: COMPUTED STYLES",
|
| 85 |
+
id:e.data.id,
|
| 86 |
+
styles:styles
|
| 87 |
+
},"*");
|
| 88 |
+
break;
|
| 89 |
+
}
|
| 90 |
+
else {
|
| 91 |
+
switch (e.data) {
|
| 92 |
+
case "RESIZE":
|
| 93 |
+
frame.style.pointerEvents='none';
|
| 94 |
+
function resizeFrame(e) {
|
| 95 |
+
if (e.clientY>0) frame.style.height=(window.innerHeight-e.clientY)+'px';
|
| 96 |
+
else frame.style.height=window.innerHeight+'px';
|
| 97 |
+
e.preventDefault();
|
| 98 |
+
e.stopPropagation();
|
| 99 |
+
return false;
|
| 100 |
+
}
|
| 101 |
+
function stop(e) {
|
| 102 |
+
frame.style.pointerEvents='all';
|
| 103 |
+
document.removeEventListener("mousemove",resizeFrame,false);
|
| 104 |
+
document.removeEventListener("mouseup",stop,false);
|
| 105 |
+
e.preventDefault();
|
| 106 |
+
e.stopPropagation();
|
| 107 |
+
return false;
|
| 108 |
+
}
|
| 109 |
+
document.addEventListener("mousemove",resizeFrame,false);
|
| 110 |
+
document.addEventListener("mouseup",stop,false);
|
| 111 |
+
break;
|
| 112 |
+
case "QUIT":
|
| 113 |
+
document.body.removeChild(frame);
|
| 114 |
+
frame=null;
|
| 115 |
+
window.removeEventListener("message",whenIreceive,false);
|
| 116 |
+
break;
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
window.addEventListener("message",whenIreceive,false);
|
| 121 |
+
}());
|