webview-pre.js 6.2 KB
Newer Older
M
Matt Bierner 已提交
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
5 6 7
// @ts-check
(function () {
	'use strict';
M
Matt Bierner 已提交
8

9
	const ipcRenderer = require('electron').ipcRenderer;
10 11


12
	const initData = {};
M
Matt Bierner 已提交
13

14 15 16 17 18 19
	function styleBody(body) {
		if (!body) {
			return
		}
		body.classList.remove('vscode-light', 'vscode-dark', 'vscode-high-contrast');
		body.classList.add(initData.activeTheme);
M
Matt Bierner 已提交
20
	}
21

22 23 24 25 26
	/**
	 * @return {HTMLIFrameElement}
	 */
	function getTarget() {
		return document.getElementById('_target');
27
	}
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

	function handleInnerClick(event) {
		if (!event || !event.view || !event.view.document) {
			return;
		}
		var node = event.target;
		while (node) {
			if (node.tagName === "A" && node.href) {
				var baseElement = event.view.document.getElementsByTagName("base")[0];
				if (node.getAttribute("href") === "#") {
					event.view.scrollTo(0, 0);
				} else if (node.hash && (node.getAttribute("href") === node.hash || (baseElement && node.href.indexOf(baseElement.href) >= 0))) {
					var scrollTarget = event.view.document.getElementById(node.hash.substr(1, node.hash.length - 1));
					if (scrollTarget) {
						scrollTarget.scrollIntoView();
					}
				} else {
					ipcRenderer.sendToHost("did-click-link", node.href);
46
				}
47 48
				event.preventDefault();
				break;
49
			}
50
			node = node.parentNode;
51 52
		}
	}
M
Matt Bierner 已提交
53 54


55 56 57 58
	document.addEventListener("DOMContentLoaded", function (event) {
		ipcRenderer.on('baseUrl', function (event, value) {
			initData.baseUrl = value;
		});
M
Matt Bierner 已提交
59

60 61 62
		ipcRenderer.on('styles', function (event, value, activeTheme) {
			initData.styles = value;
			initData.activeTheme = activeTheme;
M
Matt Bierner 已提交
63

64 65 66 67 68 69 70
			// webview
			var target = getTarget()
			if (!target) {
				return;
			}
			var body = target.contentDocument.getElementsByTagName('body');
			styleBody(body[0]);
M
Matt Bierner 已提交
71

72 73 74 75 76 77
			// iframe
			var defaultStyles = target.contentDocument.getElementById('_defaultStyles');
			if (defaultStyles) {
				defaultStyles.innerHTML = initData.styles;
			}
		});
M
Matt Bierner 已提交
78

79 80 81 82 83 84 85
		// propagate focus
		ipcRenderer.on('focus', function () {
			const target = getTarget();
			if (target) {
				target.contentWindow.focus();
			}
		});
M
Matt Bierner 已提交
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		// update iframe-contents
		ipcRenderer.on('content', function (_event, value) {
			const text = value.join('\n');
			const newDocument = new DOMParser().parseFromString(text, 'text/html');

			// know what happens here
			const stats = {
				scriptTags: newDocument.documentElement.querySelectorAll('script').length,
				inputTags: newDocument.documentElement.querySelectorAll('input').length,
				styleTags: newDocument.documentElement.querySelectorAll('style').length,
				linkStyleSheetTags: newDocument.documentElement.querySelectorAll('link[rel=stylesheet]').length,
				stringLen: text.length
			};

			// set base-url if applicable
			if (initData.baseUrl && newDocument.head.getElementsByTagName('base').length === 0) {
				const baseElement = newDocument.createElement('base');
				baseElement.href = initData.baseUrl;
				newDocument.head.appendChild(baseElement);
			}
M
Matt Bierner 已提交
107

108 109 110 111 112 113 114 115 116
			// apply default styles
			const defaultStyles = newDocument.createElement('style');
			defaultStyles.id = '_defaultStyles';
			defaultStyles.innerHTML = initData.styles;
			if (newDocument.head.hasChildNodes()) {
				newDocument.head.insertBefore(defaultStyles, newDocument.head.firstChild);
			} else {
				newDocument.head.appendChild(defaultStyles);
			}
117

118
			styleBody(newDocument.body);
M
Matt Bierner 已提交
119

120 121 122 123
			const frame = getTarget();
			if (frame) {
				frame.setAttribute('id', '_oldTarget');
			}
M
Matt Bierner 已提交
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
			// keep current scrollTop around and use later
			const scrollTop = frame && frame.contentDocument && frame.contentDocument.body ? frame.contentDocument.body.scrollTop : 0;

			const newFrame = document.createElement('iframe');
			newFrame.setAttribute('id', '_target');
			newFrame.setAttribute('frameborder', '0');
			newFrame.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
			newFrame.style.cssText = "margin: 0; overflow: hidden; position: absolute; width: 100%; height: 100%; display: none";
			document.body.appendChild(newFrame);

			// write new content onto iframe
			newFrame.contentDocument.open('text/html', 'replace');
			newFrame.contentWindow.onbeforeunload = function (e) {
				console.log('prevented webview navigation');
				return false;
			};

			newFrame.contentWindow.addEventListener('DOMContentLoaded', function (e) {
				/**
				 * @type {any}
				 */
				const contentDocument = e.target;
				if (contentDocument.body) {

					// Workaround for https://github.com/Microsoft/vscode/issues/12865
					// check new scrollTop and reset if neccessary
					if (scrollTop !== contentDocument.body.scrollTop) {
						contentDocument.body.scrollTop = scrollTop;
					}

					// Bubble out link clicks
					contentDocument.body.addEventListener('click', handleInnerClick);
157
				}
M
Matt Bierner 已提交
158

159 160 161 162 163 164
				// Clean up old frames
				[].forEach.call(document.body.getElementsByTagName('iframe'), function (frame) {
					if (frame.id !== '_target') {
						document.body.removeChild(frame);
					}
				});
165

166 167 168
				const newFrame = getTarget();
				if (newFrame.contentDocument === contentDocument) {
					newFrame.style.display = 'block';
169 170 171
				}
			});

172 173 174 175 176 177 178
			// set DOCTYPE for newDocument explicitly as DOMParser.parseFromString strips it off
			// and DOCTYPE is needed in the iframe to ensure that the user agent stylesheet is correctly overridden
			newFrame.contentDocument.write('<!DOCTYPE html>');
			newFrame.contentDocument.write(newDocument.documentElement.innerHTML);
			newFrame.contentDocument.close();

			ipcRenderer.sendToHost('did-set-content', stats);
179
		});
M
Matt Bierner 已提交
180

181 182 183 184 185 186 187
		// Forward message to the embedded iframe
		ipcRenderer.on('message', function (event, data) {
			const target = getTarget();
			if (target) {
				target.contentWindow.postMessage(data, document.location.origin);
			}
		});
188

189 190 191 192
		// forward messages from the embedded iframe
		window.onmessage = function (message) {
			ipcRenderer.sendToHost(message.data.command, message.data.data);
		};
M
Matt Bierner 已提交
193

194 195
		// signal ready
		ipcRenderer.sendToHost('webview-ready', process.pid);
196
	});
197
}());