Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.

Commit f8a5027

Browse files
authored
Merge pull request #9 from CodeMooseUS/sourcemap_crash_fix
Forward url requests to extension to fix source map crash
2 parents 96d4455 + 4d14574 commit f8a5027

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 0.0.4
2+
* Fixed a crash due to sourcemaps - [#8](https://github.com/CodeMooseUS/vscode-devtools/issues/8)
3+
4+
## 0.0.3
5+
* Fixed an issue with telemetry not updating user count correctly
6+
7+
## 0.0.2
8+
* Added devtools settings persistance - [#1](https://github.com/CodeMooseUS/vscode-devtools/issues/1)
9+
* Any settings that you change from within the devtools themselves will now be there next time you open the devtools.
10+
* This includes changing the devtools theme which auto reloads the tools.
11+
* Added anonymous telemetry reporting to see which functions need implementing next based on usage.
12+
13+
## 0.0.1
14+
* Initial preview release

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-devtools-for-chrome",
33
"displayName": "DevTools for Chrome",
44
"description": "Open the chrome devtools as a dockable webview",
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"preview": true,
77
"license": "SEE LICENSE IN LICENSE",
88
"publisher": "codemooseus",

src/extension.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ class DevToolsPanel {
257257
return this._getDevtoolsState();
258258
} else if (message.substr(0, 9) === 'setState:') {
259259
return this._setDevtoolsState(message.substr(9));
260+
} else if (message.substr(0, 7) === 'getUrl:') {
261+
return this._getDevtoolsUrl(message.substr(7));
260262
}
261263

262264
if (!this._socket) {
@@ -336,16 +338,30 @@ class DevToolsPanel {
336338
this._panel.webview.postMessage(`preferences:${JSON.stringify(allPrefs)}`);
337339
}
338340

339-
private _setDevtoolsState(state: string) {
341+
private _setDevtoolsState(message: string) {
340342
// Parse the preference from the message and store it
341-
const pref = JSON.parse(state) as { name: string, value: string };
343+
const pref = JSON.parse(message) as { name: string, value: string };
342344

343345
const allPrefsKey = 'devtools-preferences';
344346
const allPrefs: any = this._context.workspaceState.get(allPrefsKey) || {};
345347
allPrefs[pref.name] = pref.value;
346348
this._context.workspaceState.update(allPrefsKey, allPrefs);
347349
}
348350

351+
private async _getDevtoolsUrl(message: string) {
352+
// Parse the request from the message and store it
353+
const request = JSON.parse(message) as { id: number, url: string };
354+
355+
let content = '';
356+
try {
357+
content = await utils.getURL(request.url);
358+
} catch (ex) {
359+
content = '';
360+
}
361+
362+
this._panel.webview.postMessage(`setUrl:${JSON.stringify({id: request.id, content})}`);
363+
}
364+
349365
private _update() {
350366
this._panel.webview.html = this._getHtmlForWebview();
351367
}

src/host/storage.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,63 @@ class ToolsWebSocket {
6767
}
6868
}
6969

70+
class ToolsResourceLoader {
71+
private _window: Window;
72+
private _realLoadResource: (url: string) => Promise<string>;
73+
private _urlLoadNextId: number;
74+
private _urlLoadResolvers: Map<number, (url: string) => void>;
75+
76+
constructor(dtWindow: Window) {
77+
this._window = dtWindow;
78+
this._realLoadResource = (this._window as any).Runtime.loadResourcePromise;
79+
this._urlLoadNextId = 0;
80+
this._urlLoadResolvers = new Map();
81+
(this._window as any).Runtime.loadResourcePromise = this.loadResource.bind(this);
82+
}
83+
84+
public resolveUrlRequest(message: string) {
85+
// Parse the request from the message and store it
86+
const response = JSON.parse(message) as { id: number, content: string };
87+
88+
if (this._urlLoadResolvers.has(response.id)) {
89+
this._urlLoadResolvers.get(response.id)(response.content);
90+
this._urlLoadResolvers.delete(response.id);
91+
}
92+
}
93+
94+
private async loadResource(url: string): Promise<string> {
95+
if (url === 'sources/module.json') {
96+
// Override the paused event revealer so that hitting a bp will not switch to the sources tab
97+
const content = await this._realLoadResource(url);
98+
return content.replace(/{[^}]+DebuggerPausedDetailsRevealer[^}]+},/gm, '');
99+
} if (url.substr(0, 7) === 'http://' || url.substr(0, 8) === 'https://') {
100+
// Forward the cross domain request over to the extension
101+
return new Promise((resolve: (url: string) => void, reject) => {
102+
const id = this._urlLoadNextId++;
103+
this._urlLoadResolvers.set(id, resolve);
104+
window.parent.postMessage(`getUrl:${JSON.stringify({ id, url })}`, '*');
105+
});
106+
} else {
107+
return this._realLoadResource(url);
108+
}
109+
}
110+
}
111+
70112
const devToolsFrame = document.getElementById('devtools') as HTMLIFrameElement;
71113
devToolsFrame.onload = () => {
72114
const dtWindow = devToolsFrame.contentWindow;
73115

74116
// Override the apis and websocket so that we can control them
75117
(dtWindow as any).InspectorFrontendHost = new ToolsHost();
76118
(dtWindow as any).WebSocket = ToolsWebSocket;
119+
(dtWindow as any).ResourceLoaderOverride = new ToolsResourceLoader(dtWindow);
77120

78121
// Prevent the devtools from using localStorage since it doesn't exist in data uris
79122
Object.defineProperty(dtWindow, 'localStorage', {
80123
get: function () { return undefined; },
81124
set: function () { }
82125
});
83126

84-
// Override the paused event revealer so that hitting a bp will not switch to the sources tab
85-
const realLoadResource = (dtWindow as any).Runtime.loadResourcePromise as (url: string) => Promise<string>;
86-
(dtWindow as any).Runtime.loadResourcePromise = async function (url: string): Promise<string> {
87-
if (url === 'sources/module.json') {
88-
const content = await realLoadResource(url);
89-
return content.replace(/{[^}]+DebuggerPausedDetailsRevealer[^}]+},/gm, '');
90-
} else {
91-
return realLoadResource(url);
92-
}
93-
};
94-
95127
// Add unhandled exception listeners for telemetry
96128
const reportError = function (name: string, stack: string) {
97129
const telemetry = {
@@ -115,5 +147,7 @@ devToolsFrame.onload = () => {
115147
window.addEventListener('message', (e) => {
116148
if (e.data.substr(0, 12) === 'preferences:') {
117149
(devToolsFrame.contentWindow as any).InspectorFrontendHost.fireGetStateCallback(e.data.substr(12));
150+
} else if (e.data.substr(0, 7) === 'setUrl:') {
151+
(devToolsFrame.contentWindow as any).ResourceLoaderOverride.resolveUrlRequest(e.data.substr(7));
118152
}
119153
});

0 commit comments

Comments
 (0)