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

37 lines
1.1 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";
export function open_preview(url: string) {
console.log("Open URL as preview: %s", url);
const window = new electron.BrowserWindow();
window.loadURL(url);
//FIXME try catch?
const inject_file = path.join(path.dirname(module.filename), "inject.js");
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();
}
});