TeaSpeak-Client/modules/core/url-preview/index.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-06-26 16:09:01 -04:00
import * as electron from "electron";
import * as fs from "fs";
import * as path from "path";
2019-06-30 11:24:10 -04:00
import * as winmgr from "../window";
2019-06-26 16:09:01 -04:00
2019-06-30 11:24:10 -04:00
export async function open_preview(url: string) {
2019-06-26 16:09:01 -04:00
console.log("Open URL as preview: %s", url);
2019-06-30 11:24:10 -04:00
const window = new electron.BrowserWindow({
webPreferences: {
webSecurity: true,
nodeIntegration: false,
nodeIntegrationInWorker: false,
allowRunningInsecureContent: false,
},
skipTaskbar: true,
center: true,
});
await winmgr.apply_bounds('url-preview', window);
winmgr.track_bounds('url-preview', window);
window.setMenu(null);
window.loadURL(url).then(() => {
window.webContents.openDevTools();
});
2019-06-26 16:09:01 -04:00
//FIXME try catch?
const inject_file = path.join(path.dirname(module.filename), "inject.js");
2019-06-30 11:24:10 -04:00
const code_inject = fs.readFileSync(inject_file).toString();
2019-06-26 16:09:01 -04:00
window.webContents.once('dom-ready', e => {
const code_inject = fs.readFileSync(inject_file).toString();
window.webContents.executeJavaScript(code_inject, true);
});
}
electron.ipcMain.on('preview-action', (event, args) => {
const sender: electron.WebContents = event.sender;
if(!args || !args.action) {
console.warn("Received preview action without a valid action type!");
return;
}
if(args.action === "open-url") {
console.log("Opening " +args.url);
electron.shell.openExternal(args.url, {
activate: true
});
const browser = electron.BrowserWindow.fromWebContents(sender);
if(!browser)
console.warn("Failed to find browser handle");
else
browser.close();
}
});