TeaSpeak-Client/modules/core/url-preview/index.ts
2019-06-30 17:24:10 +02:00

55 lines
1.7 KiB
TypeScript

import * as electron from "electron";
import * as fs from "fs";
import * as path from "path";
import * as winmgr from "../window";
export async function open_preview(url: string) {
console.log("Open URL as preview: %s", url);
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();
});
//FIXME try catch?
const inject_file = path.join(path.dirname(module.filename), "inject.js");
const code_inject = fs.readFileSync(inject_file).toString();
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();
}
});