|
1 |
| -console.log("I am here"); |
| 1 | +console.log("I am here..."); |
| 2 | + |
| 3 | +String.prototype.times = function(n) { return n < 1 ? '':Array(n+1).join(this); }; |
| 4 | +function ArrayCopy(arr) { return Array.prototype.slice.call(arr); } |
| 5 | + |
| 6 | +// hack not to get called twice during reload/load page... (happends only for jsk) |
| 7 | +if (!document.domPageCalled) { |
| 8 | + diffDOMPage(); |
| 9 | +} |
| 10 | +document.domPageCalled = 'true'; |
| 11 | + |
| 12 | +function equalishString(a, b) { |
| 13 | + if (a === b) return 'identical'; |
| 14 | + if (a == b) return 'equal'; |
| 15 | + if (a === undefined || b === undefined) return false; |
| 16 | + if (a.trim() == b.trim()) return 'trimEqual'; |
| 17 | + |
| 18 | + var sa = a.replace(/\s/g, ''); |
| 19 | + var sb = b.replace(/\s/g, ''); |
| 20 | + if (sa == sb) return 'nospaceEqual'; |
| 21 | + if (a.length != b.length) return false; //'difflen(' + a.length + ', ' + b.length + ')'; |
| 22 | + if (a > b) return false; //"bigger(a, b)"; |
| 23 | + if (a < b) return false; //"less(a, b)"; |
| 24 | + return false //'different?'; |
| 25 | +} |
| 26 | + |
| 27 | +function diffDOMPage() { |
| 28 | + var prevDiv = document.createElement("div"); |
| 29 | + prevDiv.innerHTML = localStorage.getItem(document.URL); |
| 30 | + |
| 31 | + var prev = prevDiv; |
| 32 | + var curr = document.body; |
| 33 | + |
| 34 | + diffDOM(prev, curr, function(event, level, prev, curr) { |
| 35 | + // TODO: make this annotate the DOM of the page to highlight changes |
| 36 | + switch (event) { |
| 37 | + case 'changed': { |
| 38 | + curr.style.backgroundColor = 'lightblue'; |
| 39 | + console.log('---DIFF.changed:', prev, curr, (prev.innerText == curr.innerText), equalishString(prev.innerText, curr.innerText)); |
| 40 | + console.log(' DIFF.changed.prev:\n', prev.innerText) |
| 41 | + console.log(' DIFF.changed.curr:\n', curr.innerText); |
| 42 | + break; |
| 43 | + } |
| 44 | + case 'added' : { |
| 45 | + curr.style.backgroundColor = 'lightgreen'; |
| 46 | + console.log('---DIFF.added:', curr); |
| 47 | + console.log(' DIFF.added.curr:\n', curr.innerText); |
| 48 | + break; |
| 49 | + } |
| 50 | + case 'removed': { |
| 51 | + // TODO: consider to insert removed element with lightred color... |
| 52 | + //curr.style.backgroundColor = 'pink'; |
| 53 | + console.log('---DIFF.removed:', prev); |
| 54 | + console.log(' DIFF.removed.prev:\n', prev.innerText); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function diffDOMConsole(prev, curr) { |
| 62 | + diffDOM(prev, curr, function(event, level, prev, curr) { |
| 63 | + // TODO: make this annotate the DOM of the page to highlight changes |
| 64 | + switch (event) { |
| 65 | + case 'changed': { |
| 66 | + console.log('---DIFF.changed:', prev, curr); |
| 67 | + console.log(' DIFF.changed.prev:\n', prev.innerText) |
| 68 | + console.log(' DIFF.changed.curr:\n', curr.innerText); |
| 69 | + break; |
| 70 | + } |
| 71 | + case 'added' : { |
| 72 | + console.log('---DIFF.added:', curr); |
| 73 | + console.log(' DIFF.added.curr:\n', curr.innerText); |
| 74 | + break; |
| 75 | + } |
| 76 | + case 'removed': { |
| 77 | + console.log('---DIFF.removed:', prev); |
| 78 | + console.log(' DIFF.removed.prev:\n', prev.innerText); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function diffDOM(prev, curr, diffNotifier) { |
| 86 | + console.log("============================================================"); |
| 87 | + console.log("diffDOM: ", prev, curr); |
| 88 | + console.log("DOM: ", document); |
| 89 | + return pTraverse(0, prev, curr, diffNotifier); |
| 90 | +} |
| 91 | + |
| 92 | +function notHtml(node) { |
| 93 | + return (!node) |
| 94 | + || (node instanceof Function) |
| 95 | + || (node instanceof Number) |
| 96 | + || (node instanceof String); |
| 97 | +} |
| 98 | + |
| 99 | +function pTraverse(level, prev, curr, diffNotifier) { |
| 100 | + if (notHtml(prev) || notHtml(curr)) return; |
| 101 | + |
| 102 | + var prevC = ArrayCopy(prev.childNodes); |
| 103 | + if (!prevC) return; |
| 104 | + |
| 105 | + var currC = ArrayCopy(curr.childNodes); |
| 106 | + if (!currC) return; |
| 107 | + |
| 108 | + console.log("pTraverse: ", prev.childNodes, curr.childNodes); |
| 109 | + |
| 110 | + // Find changed or added this one DOES recurse |
| 111 | + var nPrev = prevC.shift(); |
| 112 | + var nCurr = currC.shift(); |
| 113 | + while (prevC.length || currC.length) { |
| 114 | + console.log("pTraverse.Q: ", nPrev, nCurr, prevC, currC); |
| 115 | + if (nCurr && !(nCurr instanceof HTMLElement)) { |
| 116 | + // nasty mess |
| 117 | + //if (!nPrev || prevC.length < currC.length) { |
| 118 | + //nCurr = currC.shift(); |
| 119 | + //} else if (!nCurr || prevC.length > currC.length) { |
| 120 | + // nPrev = prevC.shift(); |
| 121 | + //} else |
| 122 | + { |
| 123 | + nPrev = prevC.shift(); |
| 124 | + nCurr = currC.shift(); |
| 125 | + } |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // if no change don't go down rabbit hole |
| 130 | + if (nPrev && nCurr && equalishString(nPrev.innerText, nCurr.innerText)) { |
| 131 | + nPrev = prevC.shift(); |
| 132 | + nCurr = currC.shift(); |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + if (!nPrev || prevC.length < currC.length) { |
| 137 | + // assume something added |
| 138 | + |
| 139 | + diffNotifier('added', level, null, nCurr); |
| 140 | + // try to sync up by "ignoring" changed/added item |
| 141 | + nCurr = currC.shift(); |
| 142 | + continue; // sync up |
| 143 | + } else if (!nCurr || prevC.length > currC.length) { |
| 144 | + // assume something removed |
| 145 | + |
| 146 | + if (nPrev) diffNotifier('removed', level, nPrev, nCurr); |
| 147 | + // try to sync up by "ignoring" changed/removed item |
| 148 | + nPrev = prevC.shift(); |
| 149 | + continue; // sync up |
| 150 | + } else { |
| 151 | + // same length, assume changed |
| 152 | + |
| 153 | + diffNotifier('changed', level, nPrev, nCurr); |
| 154 | + pTraverse(level+1, nPrev, nCurr, diffNotifier); |
| 155 | + |
| 156 | + nPrev = prevC.shift(); |
| 157 | + nCurr = currC.shift(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // TODO: if any remaining stuff in prevC report as removed |
| 162 | +} |
| 163 | + |
| 164 | +console.log("I am outta here!"); |
| 165 | + |
| 166 | + |
0 commit comments