2023-05-25 01:09:17 +00:00
|
|
|
/* global chrome */
|
2020-03-05 14:56:21 +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-03-05 14:56:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 01:09:17 +00:00
|
|
|
async function exec (name) {
|
|
|
|
let result;
|
2020-03-05 14:56:21 +00:00
|
|
|
switch (name) {
|
|
|
|
case 'getMessage':
|
|
|
|
result = {
|
|
|
|
id: chrome.i18n.getMessage('@@extension_id'),
|
2023-05-25 01:09:17 +00:00
|
|
|
name: chrome.i18n.getMessage('extName')
|
|
|
|
};
|
|
|
|
break;
|
2020-03-05 14:56:21 +00:00
|
|
|
case 'getAcceptLanguages':
|
2023-05-25 01:09:17 +00:00
|
|
|
result = await new Promise(resolve => chrome.i18n.getAcceptLanguages(resolve));
|
|
|
|
break;
|
2020-03-05 14:56:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 01:09:17 +00:00
|
|
|
const funcStr = `() => { require('electron').ipcRenderer.send('success', ${JSON.stringify(result)}) }`;
|
|
|
|
evalInMainWorld(funcStr);
|
2020-03-05 14:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', event => {
|
2023-05-25 01:09:17 +00:00
|
|
|
exec(event.data.name);
|
|
|
|
});
|
2020-03-05 14:56:21 +00:00
|
|
|
|
|
|
|
evalInMainWorld(() => {
|
2023-05-25 01:09:17 +00:00
|
|
|
window.exec = name => window.postMessage({ name });
|
|
|
|
});
|