2020-03-20 20:28:31 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
|
|
|
|
import * as url from 'url';
|
2018-09-22 12:28:50 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
import { Event } from '@electron/internal/renderer/extensions/event';
|
2016-05-28 01:26:41 +00:00
|
|
|
|
2016-05-28 07:01:16 +00:00
|
|
|
class Tab {
|
2019-02-19 13:09:44 +00:00
|
|
|
public id: number
|
|
|
|
|
|
|
|
constructor (tabId: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.id = tabId;
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessageSender {
|
2019-02-19 13:09:44 +00:00
|
|
|
public tab: Tab | null
|
|
|
|
public id: string
|
|
|
|
public url: string
|
|
|
|
|
|
|
|
constructor (tabId: number, extensionId: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.tab = tabId ? new Tab(tabId) : null;
|
|
|
|
this.id = extensionId;
|
|
|
|
this.url = `chrome-extension://${extensionId}`;
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Port {
|
2019-02-19 13:09:44 +00:00
|
|
|
public disconnected: boolean = false
|
|
|
|
public onDisconnect = new Event()
|
|
|
|
public onMessage = new Event()
|
|
|
|
public sender: MessageSender
|
2016-05-28 01:26:41 +00:00
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
constructor (public tabId: number, public portId: number, extensionId: string, public name: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.onDisconnect = new Event();
|
|
|
|
this.onMessage = new Event();
|
|
|
|
this.sender = new MessageSender(tabId, extensionId);
|
2016-05-28 01:26:41 +00:00
|
|
|
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.once(`CHROME_PORT_DISCONNECT_${portId}`, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._onDisconnect();
|
|
|
|
});
|
2019-02-19 13:09:44 +00:00
|
|
|
|
|
|
|
ipcRendererInternal.on(`CHROME_PORT_POSTMESSAGE_${portId}`, (
|
2019-07-15 10:05:09 +00:00
|
|
|
_event: Electron.Event, message: any
|
2019-02-19 13:09:44 +00:00
|
|
|
) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const sendResponse = function () { console.error('sendResponse is not implemented'); };
|
|
|
|
this.onMessage.emit(JSON.parse(message), this.sender, sendResponse);
|
|
|
|
});
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect () {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (this.disconnected) return;
|
2016-05-28 08:51:49 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRendererInternal.sendToAll(this.tabId, `CHROME_PORT_DISCONNECT_${this.portId}`);
|
|
|
|
this._onDisconnect();
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 10:05:09 +00:00
|
|
|
postMessage (message: any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRendererInternal.sendToAll(this.tabId, `CHROME_PORT_POSTMESSAGE_${this.portId}`, JSON.stringify(message));
|
2016-05-28 03:07:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:37:44 +00:00
|
|
|
_onDisconnect () {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.disconnected = true;
|
|
|
|
ipcRendererInternal.removeAllListeners(`CHROME_PORT_POSTMESSAGE_${this.portId}`);
|
|
|
|
this.onDisconnect.emit();
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 06:37:44 +00:00
|
|
|
// Inject chrome API to the |context|
|
2019-03-26 02:38:35 +00:00
|
|
|
export function injectTo (extensionId: string, context: any) {
|
2019-07-31 21:25:41 +00:00
|
|
|
if (process.electronBinding('features').isExtensionsEnabled()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Attempted to load JS chrome-extension polyfill with //extensions support enabled');
|
2019-07-31 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const chrome = context.chrome = context.chrome || {};
|
2016-05-28 06:37:44 +00:00
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
ipcRendererInternal.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, (
|
|
|
|
_event: Electron.Event, tabId: number, portId: number, connectInfo: { name: string }
|
|
|
|
) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.runtime.onConnect.emit(new Port(tabId, portId, extensionId, connectInfo.name));
|
|
|
|
});
|
2016-05-28 12:35:07 +00:00
|
|
|
|
2019-03-26 02:38:35 +00:00
|
|
|
ipcRendererUtils.handle(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (
|
|
|
|
_event: Electron.Event, tabId: number, message: string
|
2019-02-19 13:09:44 +00:00
|
|
|
) => {
|
2019-03-26 02:38:35 +00:00
|
|
|
return new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId), resolve);
|
|
|
|
});
|
|
|
|
});
|
2016-05-28 12:35:07 +00:00
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
ipcRendererInternal.on('CHROME_TABS_ONCREATED', (_event: Electron.Event, tabId: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.tabs.onCreated.emit(new Tab(tabId));
|
|
|
|
});
|
2016-05-29 02:50:14 +00:00
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
ipcRendererInternal.on('CHROME_TABS_ONREMOVED', (_event: Electron.Event, tabId: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.tabs.onRemoved.emit(tabId);
|
|
|
|
});
|
2016-05-29 02:50:14 +00:00
|
|
|
|
2016-05-28 06:37:44 +00:00
|
|
|
chrome.runtime = {
|
2016-06-06 21:42:07 +00:00
|
|
|
id: extensionId,
|
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/runtime#method-getURL
|
|
|
|
getURL: function (path: string) {
|
2016-05-28 06:37:44 +00:00
|
|
|
return url.format({
|
|
|
|
protocol: 'chrome-extension',
|
|
|
|
slashes: true,
|
|
|
|
hostname: extensionId,
|
|
|
|
pathname: path
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-05-28 06:37:44 +00:00
|
|
|
},
|
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/runtime#method-getManifest
|
2019-02-13 17:16:12 +00:00
|
|
|
getManifest: function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const manifest = ipcRendererUtils.invokeSync('CHROME_EXTENSION_MANIFEST', extensionId);
|
|
|
|
return manifest;
|
2019-02-13 17:16:12 +00:00
|
|
|
},
|
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/runtime#method-connect
|
|
|
|
connect (...args: Array<any>) {
|
2016-05-28 06:37:44 +00:00
|
|
|
// Parse the optional args.
|
2020-03-20 20:28:31 +00:00
|
|
|
let targetExtensionId = extensionId;
|
|
|
|
let connectInfo = { name: '' };
|
2016-05-28 06:37:44 +00:00
|
|
|
if (args.length === 1) {
|
2019-10-30 05:46:52 +00:00
|
|
|
if (typeof args[0] === 'string') {
|
2020-03-20 20:28:31 +00:00
|
|
|
targetExtensionId = args[0];
|
2019-10-30 05:46:52 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
connectInfo = args[0];
|
2019-10-30 05:46:52 +00:00
|
|
|
}
|
2016-05-28 06:37:44 +00:00
|
|
|
} else if (args.length === 2) {
|
2020-03-20 20:28:31 +00:00
|
|
|
[targetExtensionId, connectInfo] = args;
|
2016-05-28 06:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { tabId, portId } = ipcRendererUtils.invokeSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo);
|
|
|
|
return new Port(tabId, portId, extensionId, connectInfo.name);
|
2016-05-28 12:23:43 +00:00
|
|
|
},
|
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/runtime#method-sendMessage
|
|
|
|
sendMessage (...args: Array<any>) {
|
2019-03-26 02:38:35 +00:00
|
|
|
// Parse the optional args.
|
2020-03-20 20:28:31 +00:00
|
|
|
const targetExtensionId = extensionId;
|
|
|
|
let message: string;
|
|
|
|
let options: Object | undefined;
|
|
|
|
let responseCallback: Chrome.Tabs.SendMessageCallback = () => {};
|
2019-03-26 02:38:35 +00:00
|
|
|
|
|
|
|
if (typeof args[args.length - 1] === 'function') {
|
2020-03-20 20:28:31 +00:00
|
|
|
responseCallback = args.pop();
|
2016-05-29 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 12:23:43 +00:00
|
|
|
if (args.length === 1) {
|
2020-03-20 20:28:31 +00:00
|
|
|
[message] = args;
|
2016-05-28 12:23:43 +00:00
|
|
|
} else if (args.length === 2) {
|
2019-03-26 02:38:35 +00:00
|
|
|
if (typeof args[0] === 'string') {
|
2020-03-20 20:28:31 +00:00
|
|
|
[extensionId, message] = args;
|
2016-06-22 11:47:07 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
[message, options] = args;
|
2016-06-22 11:47:07 +00:00
|
|
|
}
|
2016-05-28 12:23:43 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
[extensionId, message, options] = args;
|
2019-03-26 02:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('options are not supported');
|
2016-05-28 12:23:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRendererInternal.invoke('CHROME_RUNTIME_SEND_MESSAGE', targetExtensionId, message).then(responseCallback);
|
2016-05-28 12:23:43 +00:00
|
|
|
},
|
|
|
|
|
2016-05-29 02:50:14 +00:00
|
|
|
onConnect: new Event(),
|
|
|
|
onMessage: new Event(),
|
|
|
|
onInstalled: new Event()
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-05-28 01:26:41 +00:00
|
|
|
|
2016-05-28 07:41:12 +00:00
|
|
|
chrome.tabs = {
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/tabs#method-executeScript
|
|
|
|
executeScript (
|
|
|
|
tabId: number,
|
|
|
|
details: Chrome.Tabs.ExecuteScriptDetails,
|
2019-03-26 02:38:35 +00:00
|
|
|
resultCallback: Chrome.Tabs.ExecuteScriptCallback = () => {}
|
2019-02-19 13:09:44 +00:00
|
|
|
) {
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcRendererInternal.invoke('CHROME_TABS_EXECUTE_SCRIPT', tabId, extensionId, details)
|
2020-03-20 20:28:31 +00:00
|
|
|
.then((result: any) => resultCallback([result]));
|
2016-05-28 12:35:07 +00:00
|
|
|
},
|
|
|
|
|
2019-02-19 13:09:44 +00:00
|
|
|
// https://developer.chrome.com/extensions/tabs#method-sendMessage
|
|
|
|
sendMessage (
|
|
|
|
tabId: number,
|
|
|
|
message: any,
|
|
|
|
_options: Chrome.Tabs.SendMessageDetails,
|
2019-03-26 02:38:35 +00:00
|
|
|
responseCallback: Chrome.Tabs.SendMessageCallback = () => {}
|
2019-02-19 13:09:44 +00:00
|
|
|
) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRendererInternal.invoke('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, message).then(responseCallback);
|
2016-05-28 12:45:23 +00:00
|
|
|
},
|
|
|
|
|
2016-05-29 02:50:14 +00:00
|
|
|
onUpdated: new Event(),
|
|
|
|
onCreated: new Event(),
|
|
|
|
onRemoved: new Event()
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-05-28 07:41:12 +00:00
|
|
|
|
2016-05-28 06:37:44 +00:00
|
|
|
chrome.extension = {
|
2016-05-18 12:19:50 +00:00
|
|
|
getURL: chrome.runtime.getURL,
|
2016-05-28 12:13:00 +00:00
|
|
|
connect: chrome.runtime.connect,
|
2016-05-28 12:23:43 +00:00
|
|
|
onConnect: chrome.runtime.onConnect,
|
|
|
|
sendMessage: chrome.runtime.sendMessage,
|
|
|
|
onMessage: chrome.runtime.onMessage
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-05-18 12:19:50 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.storage = require('@electron/internal/renderer/extensions/storage').setup(extensionId);
|
2016-05-18 12:19:50 +00:00
|
|
|
|
|
|
|
chrome.pageAction = {
|
2016-05-28 12:13:00 +00:00
|
|
|
show () {},
|
|
|
|
hide () {},
|
|
|
|
setTitle () {},
|
|
|
|
getTitle () {},
|
|
|
|
setIcon () {},
|
|
|
|
setPopup () {},
|
|
|
|
getPopup () {}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-06-07 22:34:17 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
chrome.i18n = require('@electron/internal/renderer/extensions/i18n').setup(extensionId);
|
|
|
|
chrome.webNavigation = require('@electron/internal/renderer/extensions/web-navigation').setup();
|
2019-03-31 00:36:13 +00:00
|
|
|
|
|
|
|
// Electron has no concept of a browserAction but we should stub these APIs for compatibility
|
|
|
|
chrome.browserAction = {
|
|
|
|
setIcon () {},
|
|
|
|
setPopup () {}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-05-28 01:26:41 +00:00
|
|
|
}
|