diff --git a/extensions/markdown/preview-src/csp.ts b/extensions/markdown/preview-src/csp.ts index a0891955ef1..007e7e30fbb 100644 --- a/extensions/markdown/preview-src/csp.ts +++ b/extensions/markdown/preview-src/csp.ts @@ -3,45 +3,45 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -(function () { - const settings = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-settings')); - const strings = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-strings')); +import { getSettings } from './settings'; - let didShow = false; +const strings = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-strings')); +const settings = getSettings(); - const showCspWarning = () => { - if (didShow || settings.disableSecurityWarnings) { - return; - } - didShow = true; +let didShow = false; - const notification = document.createElement('a'); - notification.innerText = strings.cspAlertMessageText; - notification.setAttribute('id', 'code-csp-warning'); - notification.setAttribute('title', strings.cspAlertMessageTitle); +const showCspWarning = () => { + if (didShow || settings.disableSecurityWarnings) { + return; + } + didShow = true; - notification.setAttribute('role', 'button'); - notification.setAttribute('aria-label', strings.cspAlertMessageLabel); - notification.onclick = () => { - window.parent.postMessage({ - type: 'command', - source: settings.source, - body: { - command: 'markdown.showPreviewSecuritySelector', - args: [settings.source] - } - }, '*'); - }; - document.body.appendChild(notification); + const notification = document.createElement('a'); + notification.innerText = strings.cspAlertMessageText; + notification.setAttribute('id', 'code-csp-warning'); + notification.setAttribute('title', strings.cspAlertMessageTitle); + + notification.setAttribute('role', 'button'); + notification.setAttribute('aria-label', strings.cspAlertMessageLabel); + notification.onclick = () => { + window.parent.postMessage({ + type: 'command', + source: settings.source, + body: { + command: 'markdown.showPreviewSecuritySelector', + args: [settings.source] + } + }, '*'); }; + document.body.appendChild(notification); +}; - document.addEventListener('securitypolicyviolation', () => { +document.addEventListener('securitypolicyviolation', () => { + showCspWarning(); +}); + +window.addEventListener('message', (event) => { + if (event && event.data && event.data.name === 'vscode-did-block-svg') { showCspWarning(); - }); - - window.addEventListener('message', (event) => { - if (event && event.data && event.data.name === 'vscode-did-block-svg') { - showCspWarning(); - } - }); -}()); \ No newline at end of file + } +}); \ No newline at end of file diff --git a/extensions/markdown/preview-src/index.ts b/extensions/markdown/preview-src/index.ts index a0a0d797275..1311315897d 100644 --- a/extensions/markdown/preview-src/index.ts +++ b/extensions/markdown/preview-src/index.ts @@ -3,6 +3,9 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +import { getSettings } from './settings'; + + // From https://remysharp.com/2010/07/21/throttling-function-calls function throttle(fn: (x: any) => any, threshhold: any, scope?: any) { threshhold = threshhold || (threshhold = 250); @@ -195,7 +198,7 @@ class ActiveLineMarker { var scrollDisabled = true; const marker = new ActiveLineMarker(); -const settings = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-settings')); +const settings = getSettings(); function onLoad() { if (settings.scrollPreviewWithEditor) { diff --git a/extensions/markdown/preview-src/loading.ts b/extensions/markdown/preview-src/loading.ts index 88d1c55f589..ae4c01f68e4 100644 --- a/extensions/markdown/preview-src/loading.ts +++ b/extensions/markdown/preview-src/loading.ts @@ -2,35 +2,35 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -(function () { - const unloadedStyles: string[] = []; +import { getSettings } from './settings'; - const settings: any = JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-settings')); +const unloadedStyles: string[] = []; - const onStyleLoadError = (event: any) => { - const source = event.target.dataset.source; - unloadedStyles.push(source); - }; +const settings = getSettings(); - window.addEventListener('DOMContentLoaded', () => { - for (const link of document.getElementsByClassName('code-user-style') as HTMLCollectionOf) { - if (link.dataset.source) { - link.onerror = onStyleLoadError; - } +const onStyleLoadError = (event: any) => { + const source = event.target.dataset.source; + unloadedStyles.push(source); +}; + +window.addEventListener('DOMContentLoaded', () => { + for (const link of document.getElementsByClassName('code-user-style') as HTMLCollectionOf) { + if (link.dataset.source) { + link.onerror = onStyleLoadError; } - }); + } +}); - window.addEventListener('load', () => { - if (!unloadedStyles.length) { - return; +window.addEventListener('load', () => { + if (!unloadedStyles.length) { + return; + } + window.parent.postMessage({ + type: 'command', + source: settings.source, + body: { + command: '_markdown.onPreviewStyleLoadError', + args: [unloadedStyles] } - window.parent.postMessage({ - type: 'command', - source: settings.source, - body: { - command: '_markdown.onPreviewStyleLoadError', - args: [unloadedStyles] - } - }, '*'); - }); -})(); \ No newline at end of file + }, '*'); +}); \ No newline at end of file diff --git a/extensions/markdown/preview-src/settings.ts b/extensions/markdown/preview-src/settings.ts new file mode 100644 index 00000000000..cab6134d011 --- /dev/null +++ b/extensions/markdown/preview-src/settings.ts @@ -0,0 +1,9 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + + +export function getSettings(): any { + return JSON.parse(document.getElementById('vscode-markdown-preview-data').getAttribute('data-settings')); +}