Open fregante opened 2 years ago
Extracted from https://github.com/pixiebrix/pixiebrix-extension/pull/2317
function printf(string: string, arguments_: string[]): string {
// eslint-disable-next-line unicorn/no-array-reduce -- Short and already described by "printf"
return arguments_.reduce(
(message, part) => message.replace("%s", part),
string
);
}
/** Wrapper around the dev tools’ `eval` function to throw proper errors */
export async function devToolsEval(code: string) {
const [response, error] = await browser.devtools.inspectedWindow.eval(code);
// Handle Dev Tools API error response
// https://developer.chrome.com/docs/extensions/reference/devtools_inspectedWindow/#method-eval
// https://github.com/pixiebrix/pixiebrix-extension/pull/999#discussion_r684370643
if (!response && error?.isError) {
throw new Error(printf(error.description, error.details));
}
return response;
}
getTabsByUrl
doesn't belong to this module:
Ideas for little functions welcome. Currently I'm just looking at tiny reusable functions that I already wrote in other repositories.