2020-09-29 16:37:13 +03:00
|
|
|
import StackFrame from './react-error-overlay/utils/stack-frame';
|
|
|
|
|
2018-12-18 02:03:08 +03:00
|
|
|
const isFF = navigator.userAgent.indexOf('Firefox') !== -1;
|
|
|
|
|
2020-09-29 16:37:13 +03:00
|
|
|
function openResource(
|
|
|
|
fileName: string,
|
|
|
|
lineNumber: number,
|
|
|
|
stackFrame: StackFrame
|
|
|
|
) {
|
2018-12-15 04:04:34 +03:00
|
|
|
const adjustedLineNumber = Math.max(lineNumber - 1, 0);
|
2020-09-29 16:37:13 +03:00
|
|
|
chrome.devtools.panels.openResource(fileName, adjustedLineNumber, ((result: {
|
|
|
|
isError?: boolean;
|
|
|
|
}) => {
|
|
|
|
//console.log("openResource callback args: ", callbackArgs);
|
|
|
|
if (result.isError) {
|
2021-06-18 06:56:36 +03:00
|
|
|
const { fileName: finalFileName, lineNumber: finalLineNumber } =
|
|
|
|
stackFrame;
|
2020-09-29 16:37:13 +03:00
|
|
|
const adjustedLineNumber = Math.max(finalLineNumber! - 1, 0);
|
|
|
|
chrome.devtools.panels.openResource(
|
|
|
|
finalFileName!,
|
|
|
|
adjustedLineNumber,
|
|
|
|
(/* result */) => {
|
|
|
|
// console.log("openResource result: ", result);
|
|
|
|
}
|
|
|
|
);
|
2018-12-15 04:04:34 +03:00
|
|
|
}
|
2020-09-29 16:37:13 +03:00
|
|
|
}) as () => void);
|
2018-12-15 04:04:34 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:37:13 +03:00
|
|
|
function openAndCloseTab(url: string) {
|
2020-08-08 23:26:39 +03:00
|
|
|
chrome.tabs.create({ url }, (tab) => {
|
2018-12-18 02:03:08 +03:00
|
|
|
const removeTab = () => {
|
|
|
|
chrome.windows.onFocusChanged.removeListener(removeTab);
|
|
|
|
if (tab && tab.id) {
|
2021-08-25 07:22:54 +03:00
|
|
|
chrome.tabs.remove(tab.id, async () => {
|
2019-01-10 21:51:14 +03:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
if (chrome.runtime.lastError) console.log(chrome.runtime.lastError);
|
2018-12-18 02:03:08 +03:00
|
|
|
else if (chrome.devtools && chrome.devtools.inspectedWindow) {
|
2021-08-25 07:22:54 +03:00
|
|
|
await chrome.tabs.update(chrome.devtools.inspectedWindow.tabId, {
|
2020-08-08 23:26:39 +03:00
|
|
|
active: true,
|
2019-01-10 21:51:14 +03:00
|
|
|
});
|
|
|
|
}
|
2018-12-18 02:03:08 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (chrome.windows) chrome.windows.onFocusChanged.addListener(removeTab);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:37:13 +03:00
|
|
|
function openInIframe(url: string) {
|
2018-12-18 02:03:08 +03:00
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
iframe.src = url;
|
2020-09-29 16:37:13 +03:00
|
|
|
iframe.style.display = 'none';
|
2018-12-18 02:03:08 +03:00
|
|
|
document.body.appendChild(iframe);
|
2020-09-29 16:37:13 +03:00
|
|
|
setTimeout(() => iframe.parentNode!.removeChild(iframe), 3000);
|
2018-12-18 02:03:08 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:37:13 +03:00
|
|
|
function openInEditor(editor: string, path: string, stackFrame: StackFrame) {
|
2018-12-15 04:04:34 +03:00
|
|
|
const projectPath = path.replace(/\/$/, '');
|
2019-01-10 21:51:14 +03:00
|
|
|
const file =
|
|
|
|
stackFrame._originalFileName ||
|
2021-06-18 06:56:36 +03:00
|
|
|
(stackFrame as unknown as { finalFileName: string }).finalFileName ||
|
2019-01-10 21:51:14 +03:00
|
|
|
stackFrame.fileName ||
|
|
|
|
'';
|
|
|
|
let filePath = /^https?:\/\//.test(file)
|
|
|
|
? file.replace(/^https?:\/\/[^/]*/, '')
|
|
|
|
: file.replace(/^\w+:\/\//, '');
|
2018-12-16 01:02:41 +03:00
|
|
|
filePath = filePath.replace(/^\/~\//, '/node_modules/');
|
2018-12-15 04:04:34 +03:00
|
|
|
const line = stackFrame._originalLineNumber || stackFrame.lineNumber || '0';
|
2019-01-10 21:51:14 +03:00
|
|
|
const column =
|
|
|
|
stackFrame._originalColumnNumber || stackFrame.columnNumber || '0';
|
2018-12-15 04:04:34 +03:00
|
|
|
let url;
|
|
|
|
|
|
|
|
switch (editor) {
|
2019-01-10 21:51:14 +03:00
|
|
|
case 'vscode':
|
|
|
|
case 'code':
|
|
|
|
url = `vscode://file/${projectPath}${filePath}:${line}:${column}`;
|
|
|
|
break;
|
2018-12-15 04:04:34 +03:00
|
|
|
case 'atom':
|
2019-01-10 21:51:14 +03:00
|
|
|
url = `atom://core/open/file?filename=${projectPath}${filePath}&line=${line}&column=${column}`;
|
|
|
|
break;
|
|
|
|
case 'webstorm':
|
|
|
|
case 'phpstorm':
|
|
|
|
case 'idea':
|
|
|
|
url = `${editor}://open?file=${projectPath}${filePath}&line=${line}&column=${column}`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// sublime, emacs, macvim, textmate + custom like https://github.com/eclemens/atom-url-handler
|
2018-12-15 04:04:34 +03:00
|
|
|
url = `${editor}://open/?url=file://${projectPath}${filePath}&line=${line}&column=${column}`;
|
|
|
|
}
|
2018-12-18 02:03:08 +03:00
|
|
|
if (chrome.devtools && !isFF) {
|
|
|
|
if (chrome.tabs) openAndCloseTab(url);
|
|
|
|
else window.open(url);
|
|
|
|
} else {
|
|
|
|
openInIframe(url);
|
|
|
|
}
|
2018-12-15 04:04:34 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:37:13 +03:00
|
|
|
export default function openFile(
|
|
|
|
fileName: string,
|
|
|
|
lineNumber: number,
|
|
|
|
stackFrame: StackFrame
|
|
|
|
) {
|
2018-12-16 00:50:11 +03:00
|
|
|
if (!chrome || !chrome.storage) return; // TODO: Pass editor settings for using outside of browser extension
|
2019-01-10 21:51:14 +03:00
|
|
|
const storage = isFF
|
|
|
|
? chrome.storage.local
|
|
|
|
: chrome.storage.sync || chrome.storage.local;
|
2021-06-18 06:56:36 +03:00
|
|
|
storage.get(
|
|
|
|
['useEditor', 'editor', 'projectPath'],
|
|
|
|
function ({ useEditor, editor, projectPath }) {
|
2019-01-10 21:51:14 +03:00
|
|
|
if (
|
2021-06-18 06:56:36 +03:00
|
|
|
useEditor &&
|
|
|
|
projectPath &&
|
|
|
|
typeof editor === 'string' &&
|
|
|
|
/^\w{1,30}$/.test(editor)
|
2019-01-10 21:51:14 +03:00
|
|
|
) {
|
2021-10-21 22:08:35 +03:00
|
|
|
openInEditor(editor.toLowerCase(), projectPath as string, stackFrame);
|
2021-06-18 06:56:36 +03:00
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
chrome.devtools &&
|
|
|
|
chrome.devtools.panels &&
|
|
|
|
!!chrome.devtools.panels.openResource
|
|
|
|
) {
|
|
|
|
openResource(fileName, lineNumber, stackFrame);
|
|
|
|
} else if (chrome.runtime && (chrome.runtime.openOptionsPage || isFF)) {
|
|
|
|
if (chrome.devtools && isFF) {
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
|
|
|
'confirm("Set the editor to open the file in?")',
|
|
|
|
(result) => {
|
|
|
|
if (!result) return;
|
|
|
|
chrome.runtime.sendMessage({ type: 'OPEN_OPTIONS' });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else if (confirm('Set the editor to open the file in?')) {
|
|
|
|
chrome.runtime.openOptionsPage();
|
|
|
|
}
|
2018-12-15 04:04:34 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 00:50:11 +03:00
|
|
|
}
|
2021-06-18 06:56:36 +03:00
|
|
|
);
|
2018-12-15 04:04:34 +03:00
|
|
|
}
|