Skip to content

Commit

Permalink
fix: improve return type
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Jan 14, 2021
1 parent c2ad81e commit f24394a
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,64 @@ import type { } from "@jxa/global-type";
const makeArr = <T extends string>(arr: T[]) => arr;

export const supportedBrowsers = makeArr(["safari", "google chrome"]);
type SupportedBrowser = typeof supportedBrowsers[number];

type Options = {
/**
* Order of checking
* @default undefined - safari, google chrome
*/
browsers: typeof supportedBrowsers;
browsers: SupportedBrowser[];
};

interface JXAReturnData {
url: string;
title: string;
browserTitle: typeof supportedBrowsers[number];
tabInfo: {
url: string;
title: string;
browserTitle: SupportedBrowser;
} | null;
// browsersNoAccess: typeof supportedBrowsers;
erroredBrowsers: typeof supportedBrowsers;
}
type ActiveTabReturnType = JXAReturnData | null;
type ActiveTabReturnType = JXAReturnData;

// do not use global vars in this function. read more in @jxa/run README
const browserTabs = (browserTitles: typeof supportedBrowsers): ActiveTabReturnType => {
// // this code runs in a different environment
// this code runs in a different environment
const erroredBrowsers: any[] = [];
let tabInfo: JXAReturnData["tabInfo"] = null;
for (const browserTitle of browserTitles) {
const browser = Application(browserTitle.toLowerCase());
const browser = Application(browserTitle);
if (!browser.running()) continue;
const foremostWindow = browser.windows[0];
return {
url: foremostWindow[browserTitle === "safari" ? "currentTab" : "activeTab"]().url(),
title: browserTitle === "safari" ? foremostWindow.name() : foremostWindow.activeTab().title(),
browserTitle
};
try {
tabInfo = {
url: foremostWindow[browserTitle === "safari" ? "currentTab" : "activeTab"]().url(),
title: browserTitle === "safari" ? foremostWindow.name() : foremostWindow.activeTab().title(),
browserTitle
};
break;
} catch (err) {
// It could be "Message not understood."
// const { message } = err;
// if "An error occurred." there is no access to the browser
erroredBrowsers.push(browserTitle);
continue;
}
}
return null;
if (erroredBrowsers.length === browserTitles.length) {
throw new Error("all-browsers-failed");
}
return {
erroredBrowsers,
tabInfo
};
};

const getActiveTabInfo = async ({ browsers = supportedBrowsers }: Partial<Options> = {}): Promise<ActiveTabReturnType> => {
if (process.platform !== "darwin") throw new Error("Only macOS is supported");
console.clear();
console.time("GET-TAB");
const result: ActiveTabReturnType = await runJxa(browserTabs, browsers);
console.log(result);
console.timeEnd("GET-TAB");
return result;
// no access code: -1743
return await runJxa(browserTabs, browsers);
};

export default getActiveTabInfo;

0 comments on commit f24394a

Please sign in to comment.