1
0
Fork 0
mirror of https://github.com/anyproto/anytype-ts.git synced 2025-06-08 14:07:01 +09:00
anytype-ts/extension/lib/util.ts
2024-01-16 14:57:47 +01:00

78 lines
No EOL
1.8 KiB
TypeScript

import { UtilData, dispatcher } from 'Lib';
import { authStore, commonStore, extensionStore } from 'Store';
import Extension from 'json/extension.json';
const INDEX_POPUP = '/popup/index.html';
const INDEX_IFRAME = '/iframe/index.html'
class Util {
extensionId () {
return Extension.clipper.id;
};
isExtension () {
return (
(location.protocol == 'chrome-extension:') &&
(location.hostname == this.extensionId())
);
};
isPopup () {
return this.isExtension() && (location.pathname == INDEX_POPUP);
};
isIframe () {
return this.isExtension() && (location.pathname == INDEX_IFRAME);
};
fromPopup (url: string) {
return url.match(INDEX_POPUP);
};
fromIframe (url: string) {
return url.match(INDEX_IFRAME);
};
sendMessage (msg: any, callBack: (response) => void) {
/* @ts-ignore */
chrome.runtime.sendMessage(msg, callBack);
};
getCurrentTab (callBack: (tab) => void) {
/* @ts-ignore */
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => callBack(tabs[0]));
};
init (serverPort: string, gatewayPort: string) {
extensionStore.serverPort = serverPort;
extensionStore.gatewayPort = gatewayPort;
dispatcher.init(`http://127.0.0.1:${serverPort}`);
commonStore.gatewaySet(`http://127.0.0.1:${gatewayPort}`);
};
authorize (appKey: string, onSuccess?: () => void, onError?: (error) => void) {
const { serverPort, gatewayPort } = extensionStore;
authStore.appKeySet(appKey);
UtilData.createSession((message: any) => {
if (message.error.code) {
if (onError) {
onError(message.error);
};
return;
};
this.sendMessage({ type: 'init', appKey, serverPort, gatewayPort }, () => {});
UtilData.createsSubscriptions(onSuccess);
});
};
optionMapper (it: any) {
return it._empty_ ? null : { ...it, object: it };
};
};
export default new Util();