Skip to content

Commit 3041d48

Browse files
authored
v2.0 (#7)
Tackle error handling + compare function code + serialise reoccurring references
1 parent fe02777 commit 3041d48

File tree

7 files changed

+104
-92
lines changed

7 files changed

+104
-92
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
Chrome devtools extension intended to display result of deep in-memory object
44
comparisons with the help of dedicated console commands.
55

6+
### Features
7+
* compare objects from multiple tabs and/or between page reloads
8+
* function code included in comparison result in form of a string, may help to see if it was altered
9+
* document, dom-elements and other non-serializable objects are filtered-out from the results
10+
* self recurring references displayed only once, the rest of occurrences are filtered-out
611

712
### API
813
```javascript
@@ -13,24 +18,28 @@ console.diffRight(right); // update object on the right side only
1318
console.diffPush(next); // shifts sides, right becomes left, next becomes right
1419
```
1520

16-
1721
### Usage basics
18-
Historicly, left side represents the old state and right side the new state.
19-
Things that are present on the left side but missing on the right side are colour-coded as red (old).
20-
Things that are missing on the left side but present on the right side are colour-coded as green (new).
21-
To track changes of the same variable in timed manner you can push it with `diffPush` command,
22+
Historically, left side represents the old state and right side the new state.
23+
* Things that are present on the left side but missing on the right side are colour-coded as red (old).
24+
* Things that are missing on the left side but present on the right side are colour-coded as green (new).
25+
26+
To track changes of the same variable in timed manner you can push it with `diffPush` or `diff`
27+
with a single argument,
2228
that will shift objects from right to left, showing differences with previous push state.
23-
You can compare objects from different tabs (sites).
2429

30+
### How it works
31+
* `jsdiff-devtools` registers devtools panel
32+
* injects console commands that send data to `jsdiff-proxy`
33+
* injects `jsdiff-proxy` to farther communicate objects to the extension's `jsdiff-background`
34+
* when `console.diff` command invoked
35+
* argument/s are cloned in a suitable form for sending between different window contexts and sent to `jsdiff-proxy`
36+
* `jsdiff-proxy` catches the data and sends it to the `jsdiff-background` where it is stored for future consuming
37+
* when `jsdiff-panel` is mounted (visible in devtools) it listens to data expected to come from the `jsdiff-background`
38+
and displays it
2539

2640
### Screenshot
2741
![screenshot](./src/img/screenshot-01.png)
2842

29-
30-
### Gotcha
31-
Comparred objects shouldn't contain functions or self-recurrent references, like DOM elements or view instances have.
32-
33-
3443
### Based on
3544
- [jsondiffpatch](https://github.com/benjamine/jsondiffpatch) by Benjamín Eidelman
3645
- [vuejs](https://github.com/vuejs) by Evan You

manifest.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "console.diff(...)",
33
"description": "Deep compare complex in-memory objects inside browser devtools panel with console.diff command.",
4-
"version": "1.1",
4+
"version": "2.0",
55
"manifest_version": 2,
66
"minimum_chrome_version": "64.0",
77
"devtools_page": "src/jsdiff-devtools.html",
@@ -21,7 +21,6 @@
2121
"http://*/*",
2222
"https://*/*",
2323
"file:///*",
24-
"clipboardWrite",
25-
"tabs"
24+
"clipboardWrite"
2625
]
2726
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdiff",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "![jsdiff](./src/img/panel-icon64.png) --- Chrome devtools extension intended to display result of in-memory object comparisons with the help of dedicated commands invoked via console.",
55
"private": true,
66
"directories": {
@@ -30,7 +30,7 @@
3030
"clean-webpack-plugin": "~1.0.1",
3131
"css-loader": "~2.1.0",
3232
"file-loader": "^3.0.1",
33-
"node-sass": "~4.11.0",
33+
"node-sass": "~4.14.1",
3434
"sass-loader": "~7.1.0",
3535
"style-loader": "~0.23.1",
3636
"vue-loader": "~15.6.2",
@@ -39,8 +39,8 @@
3939
"webpack-cli": "~3.2.1"
4040
},
4141
"dependencies": {
42-
"jsondiffpatch": "~0.3.11",
43-
"moment": "~2.24.0",
44-
"vue": "~2.6.6"
42+
"jsondiffpatch": "~0.4.1",
43+
"moment": "~2.29.0",
44+
"vue": "~2.6.12"
4545
}
4646
}

src/js/bundle/jsdiff-panel.js

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/jsdiff-background.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ class Connection {
5454
*/
5555
onApiMessage(req) {
5656
console.log('jsdiff-background message relay', req);
57-
this.port.postMessage(req);
57+
if (this.port !== null) {
58+
this.port.postMessage(req);
59+
}
5860
}
5961

6062
onDisconnect() {
61-
this.req = null;
63+
this.port = null;
6264
}
6365

6466
}

src/js/jsdiff-devtools.js

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
3535
// us shown at: https://developer.chrome.com/extensions/devtools#content-script-to-devtools
3636
function injectScripts() {
3737
chrome.devtools.inspectedWindow.eval(
38-
';(' + jsdiff_devtools_extension_api.toString() + ')();', {
38+
`;(${jsdiff_devtools_extension_api.toString()})();`, {
3939
useContentScriptContext: false // default: false
4040
}, (res, error) => {
4141
if (res && !error) {
@@ -48,36 +48,84 @@ function injectScripts() {
4848
}
4949

5050
function jsdiff_devtools_extension_api() {
51-
if (console.diff) {
51+
if (typeof(console.diff) === 'function') {
5252
/*already injected*/
5353
return false;
5454
}
5555

56-
const source = 'jsdiff-devtools-extension-api';
57-
const w = window;
56+
function postDataAdapter(set, key, value) {
57+
try {
58+
if (
59+
value instanceof Element ||
60+
value instanceof Document
61+
) {
62+
return undefined;
63+
} else if (typeof(value) === 'function') {
64+
return value.toString();
65+
} else if (
66+
value instanceof Object ||
67+
typeof(value) === 'object'
68+
) {
69+
if (set.has(value)) {
70+
return undefined;
71+
}
72+
set.add(value);
73+
}
74+
75+
return value;
76+
} catch (ignore) {
77+
return undefined;
78+
}
79+
}
80+
81+
function post(payload) {
82+
try {
83+
['push', 'left', 'right'].forEach((key) => {
84+
if (payload.hasOwnProperty(key)) {
85+
let set = new Set();
86+
payload[key] = JSON.parse(
87+
JSON.stringify(
88+
payload[key],
89+
postDataAdapter.bind(null, set)
90+
)
91+
);
92+
set.clear();
93+
set = null;
94+
}
95+
});
96+
window.postMessage({payload, source: 'jsdiff-devtools-extension-api'}, '*');
97+
} catch (e) {
98+
console.error('%cJSDiff', `
99+
font-weight: 700;
100+
color: #000;
101+
background-color: yellow;
102+
padding: 2px 4px;
103+
border: 1px solid #bbb;
104+
border-radius: 4px;
105+
`, e);
106+
}
107+
}
58108

59109
Object.assign(console, {
60110
diff(left, right) {
61-
if (right === undefined) {
62-
w.postMessage({payload: {push: left}, source}, '*');
63-
}
64-
else {
65-
w.postMessage({payload: {left, right}, source}, '*');
66-
}
111+
post(right === undefined? {push: left} : {left, right});
67112
},
113+
68114
diffLeft(left) {
69-
w.postMessage({payload: {left}, source}, '*');
115+
post({left});
70116
},
117+
71118
diffRight(right) {
72-
w.postMessage({payload: {right}, source}, '*');
119+
post({right});
73120
},
121+
74122
diffPush(push) {
75-
w.postMessage({payload: {push}, source}, '*');
76-
}
123+
post({push});
124+
},
77125
});
78126

79127
console.log(
80-
'%cJSDiff API', `
128+
'%cJSDiff', `
81129
font-weight: 700;
82130
color: #000;
83131
background-color: yellow;

src/js/view/panel.vue

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<section
44
v-if="hasBothSides"
55
class="-header">
6-
6+
77
<div class="-toolbox">
88
<button
99
v-if="hasBothSides"
@@ -22,7 +22,7 @@
2222
<div class="-last-updated">
2323
<span>Last updated</span>
2424
<span class="-value" v-text="lastUpdated"/>
25-
</div>
25+
</div>
2626
</section>
2727

2828
<a class="-icon" :href="git.self" target="_blank" :title="git.self">
@@ -63,7 +63,7 @@
6363
require('jsondiffpatch/dist/formatters-styles/html.css');
6464
const Vue = require('vue').default;
6565
const moment = require('moment');
66-
66+
6767
module.exports = Vue.extend({
6868
name: 'jsdiff-panel',
6969
@@ -103,7 +103,7 @@
103103
104104
computed: {
105105
lastUpdated() {
106-
return moment(this.compare.timestamp).fromNow();
106+
return moment(this.compare.timestamp).from(this.now);
107107
},
108108
109109
hasBothSides() {
@@ -119,7 +119,6 @@
119119
120120
deltaHtml() {
121121
try {
122-
this.$_adjustArrows();
123122
return formatters.html.format(
124123
jsondiffpatch.diff(this.compare.left, this.compare.right),
125124
this.compare.left
@@ -135,7 +134,6 @@
135134
onToggleUnchanged(e) {
136135
this.showUnchanged = !this.showUnchanged;
137136
formatters.html.showUnchanged(this.showUnchanged, this.$refs.delta);
138-
this.$_adjustArrows();
139137
},
140138
141139
onCopyDelta() {
@@ -177,52 +175,6 @@
177175
178176
this.$_restartLastUpdated();
179177
},
180-
181-
$_adjustArrows() {
182-
this.$nextTick(() => {
183-
const t = document.body;
184-
var e = function(t) {
185-
return t.textContent || t.innerText;
186-
},
187-
o = function(t, e, o) {
188-
for (var a = t.querySelectorAll(e), r = 0, i = a.length; i > r; r++) {
189-
o(a[r]);
190-
}
191-
},
192-
a = function(t, e) {
193-
for (var o = 0, a = t.children.length; a > o; o++) {
194-
e(t.children[o], o);
195-
}
196-
};
197-
198-
o(t, '.jsondiffpatch-arrow', function(t) {
199-
var o = t.parentNode,
200-
r = t.children[0],
201-
i = r.children[1];
202-
r.style.display = 'none';
203-
var n,
204-
s = e(o.querySelector('.jsondiffpatch-moved-destination')),
205-
d = o.parentNode;
206-
if (a(d, function(t) {
207-
t.getAttribute('data-key') === s && (n = t);
208-
}), n) {
209-
try {
210-
var f = n.offsetTop - o.offsetTop;
211-
r.setAttribute('height', Math.abs(f) + 6);
212-
t.style.top = -8 + (f > 0 ? 0 : f) + 'px';
213-
var l = f > 0 ?
214-
'M30,0 Q-10,' + Math.round(f / 2) + ' 26,' + (f - 4) :
215-
'M30,' + -f + ' Q-10,' + Math.round(-f / 2) + ' 26,4';
216-
i.setAttribute('d', l);
217-
r.style.display = '';
218-
} catch (c) {
219-
return;
220-
}
221-
}
222-
});
223-
});
224-
}
225-
226178
}
227179
228180
});

0 commit comments

Comments
 (0)