import * as electron from "electron"; import { EventEmitter } from "events"; import * as fs from "fs"; import { getFetchUrl } from "../src/coder/api"; import { escapePath } from "../src/coder/common"; import { wush } from "../src/coder/server"; import { IKey, Dialog } from "./dialog"; (global as any).getOpenUrls = () => { return []; }; const oldCreateElement = document.createElement; document.createElement = (tagName: string) => { const createElement = (tagName: string) => { return oldCreateElement.call(document, tagName); }; if (tagName === "webview") { const view = createElement("iframe") as HTMLIFrameElement; view.style.border = "0px"; const frameID = Math.random().toString(); view.addEventListener("error", (event) => { console.log("Got iframe error", event.error, event.message); }); window.addEventListener("message", (event) => { if (!event.data || !event.data.id) { return; } if (event.data.id !== frameID) { return; } const e = new CustomEvent("ipc-message"); (e as any).channel = event.data.channel; (e as any).args = event.data.data; view.dispatchEvent(e); }); view.sandbox.add("allow-same-origin", "allow-scripts", "allow-popups", "allow-forms"); Object.defineProperty(view, "preload", { set: (url: string) => { view.onload = () => { view.contentDocument.body.id = frameID; view.contentDocument.body.parentElement.style.overflow = "hidden"; const script = document.createElement("script"); script.src = url; view.contentDocument.head.appendChild(script); }; }, }); (view as any).getWebContents = () => undefined; (view as any).send = (channel: string, ...args) => { if (args[0] && typeof args[0] === "object" && args[0].contents) { args[0].contents = (args[0].contents as string).replace(/"(file:\/\/[^"]*)"/g, (m) => `"${getFetchUrl(m)}"`); args[0].contents = (args[0].contents as string).replace(/"vscode-resource:([^"]*)"/g, (m) => `"${getFetchUrl(m)}"`); } view.contentWindow.postMessage({ channel, data: args, id: frameID, }, "*"); }; return view; } return createElement(tagName); }; const rendererToMainEmitter = new EventEmitter(); const mainToRendererEmitter = new EventEmitter(); module.exports = { clipboard: { has: () => { return false; }, writeText: (value: string) => { // Taken from https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f const active = document.activeElement as HTMLElement; const el = document.createElement('textarea'); // Create a