2023-05-25 01:09:17 +00:00
|
|
|
/* global chrome */
|
2020-09-15 16:29:32 +00:00
|
|
|
|
2023-05-25 01:09:17 +00:00
|
|
|
function evalInMainWorld (fn) {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.textContent = `((${fn})())`;
|
|
|
|
document.documentElement.appendChild(script);
|
2020-09-15 16:29:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 01:09:17 +00:00
|
|
|
async function exec (name) {
|
|
|
|
let result;
|
2020-09-15 16:29:32 +00:00
|
|
|
switch (name) {
|
|
|
|
case 'getManifest':
|
2023-05-25 01:09:17 +00:00
|
|
|
result = chrome.runtime.getManifest();
|
|
|
|
break;
|
2020-09-15 16:29:32 +00:00
|
|
|
case 'id':
|
2023-05-25 01:09:17 +00:00
|
|
|
result = chrome.runtime.id;
|
|
|
|
break;
|
2020-09-15 16:29:32 +00:00
|
|
|
case 'getURL':
|
2023-05-25 01:09:17 +00:00
|
|
|
result = chrome.runtime.getURL('main.js');
|
|
|
|
break;
|
2020-09-15 16:29:32 +00:00
|
|
|
case 'getPlatformInfo': {
|
|
|
|
result = await new Promise(resolve => {
|
2023-05-25 01:09:17 +00:00
|
|
|
chrome.runtime.sendMessage(name, resolve);
|
|
|
|
});
|
|
|
|
break;
|
2020-09-15 16:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 01:09:17 +00:00
|
|
|
const funcStr = `() => { require('electron').ipcRenderer.send('success', ${JSON.stringify(result)}) }`;
|
|
|
|
evalInMainWorld(funcStr);
|
2020-09-15 16:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', event => {
|
2023-05-25 01:09:17 +00:00
|
|
|
exec(event.data.name);
|
|
|
|
});
|
2020-09-15 16:29:32 +00:00
|
|
|
|
|
|
|
evalInMainWorld(() => {
|
2023-05-25 01:09:17 +00:00
|
|
|
window.exec = name => window.postMessage({ name });
|
|
|
|
});
|