82af000a37
* chore: cleanup eslint suppressions * address feedback * revert script/lib/azput.js * revert spec/fixtures/apps/remote-control/main.js * address feedback * revert typings/internal-ambient.d.ts
39 lines
935 B
JavaScript
39 lines
935 B
JavaScript
/* global chrome */
|
|
|
|
function evalInMainWorld (fn) {
|
|
const script = document.createElement('script');
|
|
script.textContent = `((${fn})())`;
|
|
document.documentElement.appendChild(script);
|
|
}
|
|
|
|
async function exec (name) {
|
|
let result;
|
|
switch (name) {
|
|
case 'getManifest':
|
|
result = chrome.runtime.getManifest();
|
|
break;
|
|
case 'id':
|
|
result = chrome.runtime.id;
|
|
break;
|
|
case 'getURL':
|
|
result = chrome.runtime.getURL('main.js');
|
|
break;
|
|
case 'getPlatformInfo': {
|
|
result = await new Promise(resolve => {
|
|
chrome.runtime.sendMessage(name, resolve);
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|
|
const funcStr = `() => { require('electron').ipcRenderer.send('success', ${JSON.stringify(result)}) }`;
|
|
evalInMainWorld(funcStr);
|
|
}
|
|
|
|
window.addEventListener('message', event => {
|
|
exec(event.data.name);
|
|
});
|
|
|
|
evalInMainWorld(() => {
|
|
window.exec = name => window.postMessage({ name });
|
|
});
|