Skip to content

Commit 2b9d8fb

Browse files
committed
migrate to manifest version 3
1 parent 222788b commit 2b9d8fb

9 files changed

+162
-194
lines changed

README.md

+48-20
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,58 @@
55
Chrome devtools extension intended to display result of deep in-memory object
66
comparisons with the help of dedicated console commands.
77

8+
### Based on
9+
10+
- [jsondiffpatch](https://github.com/benjamine/jsondiffpatch) by Benjamín Eidelman
11+
- [vuejs](https://github.com/vuejs) by Evan You
12+
813
### Features
914

1015
- compare objects from multiple tabs and/or between page reloads
1116
- function code included in comparison result in form of a string, may help to see if it was altered
1217
- document, dom-elements and other non-serializable objects are filtered-out from the results
1318
- self recurring references displayed only once, the rest of occurrences are filtered-out
1419

20+
### Limitations and workarounds
21+
22+
- some instances of objects may cause exception during preparations for comparison
23+
- try to narrow compared contexts
24+
- if it's some Browser API that causes an exception and not a framework, consider opening an issue,
25+
so it will be possible to solve is on a permanent basis
26+
- while paused in debug mode, JSDiff panel won't reflect the result until runtime is resumed ([#10][i10])
27+
28+
[i10]: https://github.com/zendive/jsdiff/issues/10
29+
1530
### API
1631

32+
- **console.diff(left, right)** - compare left and right arguments
33+
34+
```javascript
35+
console.diff({ a: 1, b: 1, c: 3 }, { a: 1, b: 2, d: 3 });
36+
```
37+
38+
- **console.diffPush(next)** - shifts sides, right becomes left, next becomes right
39+
40+
```javascript
41+
console.diffPush(Date.now());
42+
```
43+
44+
- **console.diff(next)** - shorthand for `diffPush`
45+
46+
```javascript
47+
console.diff(Date.now());
48+
```
49+
50+
- **console.diffLeft(left)** - update the old value
51+
1752
```javascript
18-
console.diff(left, right); // compare left and right
19-
console.diff(next); // shorthand of diffPush while single argumented
20-
console.diffLeft(left); // update object on the left side only
21-
console.diffRight(right); // update object on the right side only
22-
console.diffPush(next); // shifts sides, right becomes left, next becomes right
53+
console.diffLeft(Date.now());
54+
```
55+
56+
- **console.diffRight(right)** - update the new value
57+
58+
```javascript
59+
console.diffRight(Date.now());
2360
```
2461

2562
### Usage basics
@@ -30,25 +67,16 @@ Historically, left side represents the old state and right side the new state.
3067
- Things that are missing on the left side but present on the right side are colour-coded as green (new).
3168

3269
To track changes of the same variable in timed manner you can push it with `diffPush` or `diff`
33-
with a single argument,
34-
that will shift objects from right to left, showing differences with previous push state.
70+
with a single argument, that will shift objects from right to left, showing differences with previous push state.
3571

3672
### How it works
3773

38-
- `jsdiff-devtools` registers devtools panel
39-
- injects console commands that send data to `jsdiff-proxy`
40-
- injects `jsdiff-proxy` to farther communicate objects to the extension's `jsdiff-background`
41-
- when `console.diff` command invoked
42-
- argument/s are cloned in a suitable form for sending between different window contexts and sent to `jsdiff-proxy`
43-
- `jsdiff-proxy` catches the data and sends it to the `jsdiff-background` where it is stored for future consuming
44-
- when `jsdiff-panel` is mounted (visible in devtools) it listens to data expected to come from the `jsdiff-background`
45-
and displays it
74+
- `jsdiff-devtools.js` registers devtools panel
75+
- injects `console.diff` commands into inspected window's console interface
76+
- each function stringifies arguments and sends them via `postMessage` to `jsdiff-proxy.js`
77+
- injects `jsdiff-proxy.js` that listens on window message and sends it further to chrome runtime.
78+
- when `jsdiff-panel.js` is visible in devtools, it reflects result of last invocation and listens for future messages
4679

4780
### Screenshot
4881

4982
![screenshot](./src/img/screenshot-01.png)
50-
51-
### Based on
52-
53-
- [jsondiffpatch](https://github.com/benjamine/jsondiffpatch) by Benjamín Eidelman
54-
- [vuejs](https://github.com/vuejs) by Evan You

manifest.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{
22
"name": "console.diff(...)",
3-
"description": "Deep compare complex in-memory objects inside browser devtools panel with console.diff command.",
3+
"description": "Compare in-memory objects and see the result inside devtools panel with a set of console.diff functions.",
44
"version": "2.1",
5-
"manifest_version": 2,
5+
"manifest_version": 3,
66
"minimum_chrome_version": "64.0",
77
"devtools_page": "src/jsdiff-devtools.html",
88
"icons": {
99
"28": "src/img/panel-icon28.png",
1010
"64": "src/img/panel-icon64.png",
1111
"128": "src/img/panel-icon128.png"
1212
},
13-
"background": {
14-
"persistent": false,
15-
"scripts": ["src/js/jsdiff-background.js"]
16-
},
17-
"content_security_policy": "script-src 'self'; object-src 'self'",
18-
"permissions": ["http://*/*", "https://*/*", "clipboardWrite"]
13+
"permissions": ["storage", "scripting", "activeTab", "clipboardWrite"],
14+
"host_permissions": ["*://*/*"]
1915
}

src/js/bundle/jsdiff-panel.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/jsdiff-background.js

-67
This file was deleted.

src/js/jsdiff-devtools.js

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
// resistance is futile
2-
3-
// Create panel
41
chrome.devtools.panels.create(
52
'JSDiff',
63
'/src/img/panel-icon28.png',
74
'/src/jsdiff-panel.html',
85
(panel) => {
9-
//panel.onSearch.addListener(sendMessage('jsdiff-panel-search'));
10-
//panel.onShown.addListener(sendMessage('jsdiff-panel-shown'));
11-
//panel.onHidden.addListener(sendMessage('jsdiff-panel-hidden'));
6+
panel.onSearch.addListener(async (cmd, query) => {
7+
await chrome.runtime.sendMessage({
8+
source: 'jsdiff-panel-search',
9+
params: { cmd, query },
10+
});
11+
});
1212
}
1313
);
1414

15-
// Create a connection to the background page
15+
// tabId may be null if user opened the devtools of the devtools
1616
if (chrome.devtools.inspectedWindow.tabId !== null) {
17-
// tabId may be null if user opened the devtools of the devtools
18-
const backgroundPageConnection = chrome.runtime.connect({
19-
name: 'jsdiff-devtools',
20-
});
21-
backgroundPageConnection.postMessage({
22-
name: 'init',
23-
tabId: chrome.devtools.inspectedWindow.tabId,
24-
});
25-
2617
injectScripts();
2718
}
2819

@@ -33,6 +24,13 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
3324
}
3425
});
3526

27+
// track api invocation - (api can be invoked prior opening of jsdiff panel)
28+
chrome.runtime.onMessage.addListener(async (req) => {
29+
if ('jsdiff-devtools-extension-api' === req.source) {
30+
await chrome.storage.local.set({ lastApiReq: req });
31+
}
32+
});
33+
3634
// Inject console api and messaging proxy
3735
// us shown at: https://developer.chrome.com/extensions/devtools#content-script-to-devtools
3836
function injectScripts() {
@@ -43,18 +41,32 @@ function injectScripts() {
4341
},
4442
(res, error) => {
4543
if (res && !error) {
46-
// injected
47-
chrome.tabs.executeScript(chrome.devtools.inspectedWindow.tabId, {
48-
file: '/src/js/jsdiff-proxy.js',
49-
});
44+
const tabId = chrome.devtools.inspectedWindow.tabId;
45+
46+
chrome.scripting
47+
.executeScript({
48+
target: {
49+
tabId,
50+
allFrames: true,
51+
},
52+
files: ['/src/js/jsdiff-proxy.js'],
53+
})
54+
.then(
55+
() => {
56+
// console.log('script injected in all frames');
57+
},
58+
(error) => {
59+
// console.error('script inject failed', error);
60+
}
61+
);
5062
}
5163
}
5264
);
5365
}
5466

5567
function jsdiff_devtools_extension_api() {
5668
if (typeof console.diff === 'function') {
57-
/*already injected*/
69+
/* already injected */
5870
return false;
5971
}
6072

@@ -89,17 +101,21 @@ function jsdiff_devtools_extension_api() {
89101
set = null;
90102
}
91103
});
104+
92105
window.postMessage(
93-
{ payload, source: 'jsdiff-devtools-extension-api' },
106+
{
107+
payload,
108+
source: 'jsdiff-devtools-extension-api',
109+
},
94110
'*'
95111
);
96112
} catch (e) {
97113
console.error(
98-
'%cJSDiff',
114+
'%cconsole.diff',
99115
`
100116
font-weight: 700;
101117
color: #000;
102-
background-color: yellow;
118+
background-color: #ffbbbb;
103119
padding: 2px 4px;
104120
border: 1px solid #bbb;
105121
border-radius: 4px;
@@ -110,25 +126,15 @@ function jsdiff_devtools_extension_api() {
110126
}
111127

112128
Object.assign(console, {
113-
diff(left, right) {
114-
post(right === undefined ? { push: left } : { left, right });
115-
},
116-
117-
diffLeft(left) {
118-
post({ left });
119-
},
120-
121-
diffRight(right) {
122-
post({ right });
123-
},
124-
125-
diffPush(push) {
126-
post({ push });
127-
},
129+
diff: (left, right) =>
130+
post(right === undefined ? { push: left } : { left, right }),
131+
diffLeft: (left) => post({ left }),
132+
diffRight: (right) => post({ right }),
133+
diffPush: (push) => post({ push }),
128134
});
129135

130136
console.debug(
131-
'%c✚ console.diff(left, right);',
137+
'%c✚ console.diff',
132138
`
133139
font-weight: 700;
134140
color: #000;
@@ -141,9 +147,3 @@ function jsdiff_devtools_extension_api() {
141147

142148
return true;
143149
}
144-
145-
function sendMessage(message) {
146-
return function () {
147-
chrome.runtime.sendMessage(message);
148-
};
149-
}

src/js/jsdiff-proxy.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
(function (w, x) {
2-
'use strict';
3-
w.addEventListener('message', function jsdiff_devtools_extension_proxy(e) {
1+
(async () => {
2+
window.addEventListener('message', (e) => {
43
if (
5-
// message from the same frame
6-
e.source === w &&
7-
e.data &&
84
typeof e.data === 'object' &&
95
e.data.source === 'jsdiff-devtools-extension-api'
106
) {
11-
x && x.sendMessage(e.data);
7+
chrome.runtime.sendMessage(e.data);
128
}
139
});
14-
})(window, chrome.runtime);
10+
})();

0 commit comments

Comments
 (0)