Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
feat: detect __OPENSUMI_DEVTOOLS_GLOBAL_HOOK__ to decide if create th…
Browse files Browse the repository at this point in the history
…e panel (#49)
  • Loading branch information
tyn1998 authored Aug 20, 2022
1 parent 2643f32 commit fca2818
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
29 changes: 12 additions & 17 deletions src/capturer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ const startCapturing = () => {
return evalInWindow(() => {
// Return if messages are already being listened to prevent duplicates
// when reloading the extension
if (window.__opensumi_devtools.messages != null) {
window.__opensumi_devtools.messages = [];
if (window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages != null) {
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages = [];
return;
}

window.__opensumi_devtools.messages = [];
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages = [];

window.__opensumi_devtools.capture = (message) => {
if (window.__opensumi_devtools.evaling) return;
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.capture = (message) => {
if (window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.evaling) return;

// if the length of messages is greater than 9999, devtools window
// is regarded to be closed in capturing state. So stop capturing.
if (window.__opensumi_devtools.messages.length > 9999) {
window.__opensumi_devtools = undefined;
if (window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages.length > 9999) {
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__ = {};
return;
}

Expand Down Expand Up @@ -55,27 +55,22 @@ const startCapturing = () => {
}
}

window.__opensumi_devtools.messages.push(msg);
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages.push(msg);
};
});
};

const stopCapturing = () => {
return evalInWindow(() => {
if (window.__opensumi_devtools.messages)
delete window.__opensumi_devtools.messages;
if (window.__opensumi_devtools.capture)
delete window.__opensumi_devtools.capture;
if (window.__opensumi_devtools.latency)
delete window.__opensumi_devtools.latency;
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__ = {};
});
};

const getMessages = () => {
return evalInWindow(() => {
const messages = window.__opensumi_devtools.messages;
const messages = window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages;
// clear messages after getting them each time
if (messages) window.__opensumi_devtools.messages = [];
if (messages) window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.messages = [];
return messages;
}).then((messages) => {
if (messages) return messages;
Expand All @@ -88,7 +83,7 @@ const getMessages = () => {

const getLatency = () => {
return evalInWindow(() => {
return window.__opensumi_devtools.latency;
return window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.latency;
}).then((latency) => {
return latency;
});
Expand Down
25 changes: 20 additions & 5 deletions src/pages/Devtools/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
chrome.devtools.panels.create(
'OpenSumi DevTools',
'logo.png',
'panel.html'
);
const run = () => {
window.chrome.devtools.inspectedWindow.eval(
`window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__`,
(result, exception) => {
if (exception) {
console.log(exception);
} else {
if (result) {
chrome.devtools.panels.create(
'OpenSumi DevTools',
'logo.png',
'panel.html'
);
}
}
}
);
};

run();
5 changes: 2 additions & 3 deletions src/utils/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ const evalInWindow = (expression, ...rest) => {

expression = `
(function () {
window.__opensumi_devtools = window.__opensumi_devtools || {}
window.__opensumi_devtools.evaling = true
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.evaling = true
try {
return ${expression}
} finally {
window.__opensumi_devtools.evaling = false
window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__.evaling = false
}
})()
`;
Expand Down

0 comments on commit fca2818

Please sign in to comment.