Skip to content

Commit 3aaa42e

Browse files
authored
feat: add react devtool (#53)
1 parent 14f1e12 commit 3aaa42e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+194054
-5
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ npm-debug.log.*
3333
.erb
3434
.eslintrc.js
3535

36-
sign.js
36+
exts
37+
sign.js

exts/reactdev/build/background.js

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
40+
/******/ }
41+
/******/ };
42+
/******/
43+
/******/ // define __esModule on exports
44+
/******/ __webpack_require__.r = function(exports) {
45+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
46+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
47+
/******/ }
48+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
49+
/******/ };
50+
/******/
51+
/******/ // create a fake namespace object
52+
/******/ // mode & 1: value is a module id, require it
53+
/******/ // mode & 2: merge all properties of value into the ns
54+
/******/ // mode & 4: return value when already ns object
55+
/******/ // mode & 8|1: behave like require
56+
/******/ __webpack_require__.t = function(value, mode) {
57+
/******/ if(mode & 1) value = __webpack_require__(value);
58+
/******/ if(mode & 8) return value;
59+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
60+
/******/ var ns = Object.create(null);
61+
/******/ __webpack_require__.r(ns);
62+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
63+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
64+
/******/ return ns;
65+
/******/ };
66+
/******/
67+
/******/ // getDefaultExport function for compatibility with non-harmony modules
68+
/******/ __webpack_require__.n = function(module) {
69+
/******/ var getter = module && module.__esModule ?
70+
/******/ function getDefault() { return module['default']; } :
71+
/******/ function getModuleExports() { return module; };
72+
/******/ __webpack_require__.d(getter, 'a', getter);
73+
/******/ return getter;
74+
/******/ };
75+
/******/
76+
/******/ // Object.prototype.hasOwnProperty.call
77+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
78+
/******/
79+
/******/ // __webpack_public_path__
80+
/******/ __webpack_require__.p = "/build/";
81+
/******/
82+
/******/
83+
/******/ // Load entry module and return exports
84+
/******/ return __webpack_require__(__webpack_require__.s = 116);
85+
/******/ })
86+
/************************************************************************/
87+
/******/ ({
88+
89+
/***/ 116:
90+
/***/ (function(module, exports, __webpack_require__) {
91+
92+
"use strict";
93+
/* global chrome */
94+
95+
96+
const ports = {};
97+
const IS_FIREFOX = navigator.userAgent.indexOf('Firefox') >= 0;
98+
chrome.runtime.onConnect.addListener(function (port) {
99+
let tab = null;
100+
let name = null;
101+
102+
if (isNumeric(port.name)) {
103+
tab = port.name;
104+
name = 'devtools';
105+
installContentScript(+port.name);
106+
} else {
107+
tab = port.sender.tab.id;
108+
name = 'content-script';
109+
}
110+
111+
if (!ports[tab]) {
112+
ports[tab] = {
113+
devtools: null,
114+
'content-script': null
115+
};
116+
}
117+
118+
ports[tab][name] = port;
119+
120+
if (ports[tab].devtools && ports[tab]['content-script']) {
121+
doublePipe(ports[tab].devtools, ports[tab]['content-script']);
122+
}
123+
});
124+
125+
function isNumeric(str) {
126+
return +str + '' === str;
127+
}
128+
129+
function installContentScript(tabId) {
130+
chrome.tabs.executeScript(tabId, {
131+
file: '/build/contentScript.js'
132+
}, function () {});
133+
}
134+
135+
function doublePipe(one, two) {
136+
one.onMessage.addListener(lOne);
137+
138+
function lOne(message) {
139+
two.postMessage(message);
140+
}
141+
142+
two.onMessage.addListener(lTwo);
143+
144+
function lTwo(message) {
145+
one.postMessage(message);
146+
}
147+
148+
function shutdown() {
149+
one.onMessage.removeListener(lOne);
150+
two.onMessage.removeListener(lTwo);
151+
one.disconnect();
152+
two.disconnect();
153+
}
154+
155+
one.onDisconnect.addListener(shutdown);
156+
two.onDisconnect.addListener(shutdown);
157+
}
158+
159+
function setIconAndPopup(reactBuildType, tabId) {
160+
chrome.browserAction.setIcon({
161+
tabId: tabId,
162+
path: {
163+
'16': 'icons/16-' + reactBuildType + '.png',
164+
'32': 'icons/32-' + reactBuildType + '.png',
165+
'48': 'icons/48-' + reactBuildType + '.png',
166+
'128': 'icons/128-' + reactBuildType + '.png'
167+
}
168+
});
169+
chrome.browserAction.setPopup({
170+
tabId: tabId,
171+
popup: 'popups/' + reactBuildType + '.html'
172+
});
173+
}
174+
175+
function isRestrictedBrowserPage(url) {
176+
return !url || new URL(url).protocol === 'chrome:';
177+
}
178+
179+
function checkAndHandleRestrictedPageIfSo(tab) {
180+
if (tab && isRestrictedBrowserPage(tab.url)) {
181+
setIconAndPopup('restricted', tab.id);
182+
}
183+
} // update popup page of any existing open tabs, if they are restricted browser pages.
184+
// we can't update for any other types (prod,dev,outdated etc)
185+
// as the content script needs to be injected at document_start itself for those kinds of detection
186+
// TODO: Show a different popup page(to reload current page probably) for old tabs, opened before the extension is installed
187+
188+
189+
if (!IS_FIREFOX) {
190+
chrome.tabs.query({}, tabs => tabs.forEach(checkAndHandleRestrictedPageIfSo));
191+
chrome.tabs.onCreated.addListener((tabId, changeInfo, tab) => checkAndHandleRestrictedPageIfSo(tab));
192+
} // Listen to URL changes on the active tab and update the DevTools icon.
193+
194+
195+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
196+
if (IS_FIREFOX) {
197+
// We don't properly detect protected URLs in Firefox at the moment.
198+
// However we can reset the DevTools icon to its loading state when the URL changes.
199+
// It will be updated to the correct icon by the onMessage callback below.
200+
if (tab.active && changeInfo.status === 'loading') {
201+
setIconAndPopup('disabled', tabId);
202+
}
203+
} else {
204+
// Don't reset the icon to the loading state for Chrome or Edge.
205+
// The onUpdated callback fires more frequently for these browsers,
206+
// often after onMessage has been called.
207+
checkAndHandleRestrictedPageIfSo(tab);
208+
}
209+
});
210+
chrome.runtime.onMessage.addListener((request, sender) => {
211+
var _request$payload, _ports$id;
212+
213+
const tab = sender.tab;
214+
215+
if (tab) {
216+
const id = tab.id; // This is sent from the hook content script.
217+
// It tells us a renderer has attached.
218+
219+
if (request.hasDetectedReact) {
220+
// We use browserAction instead of pageAction because this lets us
221+
// display a custom default popup when React is *not* detected.
222+
// It is specified in the manifest.
223+
setIconAndPopup(request.reactBuildType, id);
224+
} else {
225+
switch ((_request$payload = request.payload) === null || _request$payload === void 0 ? void 0 : _request$payload.type) {
226+
case 'fetch-file-with-cache-complete':
227+
case 'fetch-file-with-cache-error':
228+
// Forward the result of fetch-in-page requests back to the extension.
229+
const devtools = (_ports$id = ports[id]) === null || _ports$id === void 0 ? void 0 : _ports$id.devtools;
230+
231+
if (devtools) {
232+
devtools.postMessage(request);
233+
}
234+
235+
break;
236+
}
237+
}
238+
}
239+
});
240+
241+
/***/ })
242+
243+
/******/ });

0 commit comments

Comments
 (0)