Update extension.js
This commit is contained in:
parent
598b0efab0
commit
acb3d3e66b
|
@ -14,7 +14,9 @@ class ErikrafTDropViewProvider {
|
||||||
this.extensionPath = extensionPath;
|
this.extensionPath = extensionPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveWebviewView(webviewView) {
|
resolveWebviewView(webviewView, context, token) {
|
||||||
|
this.webviewView = webviewView;
|
||||||
|
|
||||||
webviewView.webview.options = {
|
webviewView.webview.options = {
|
||||||
enableScripts: true,
|
enableScripts: true,
|
||||||
localResourceRoots: [
|
localResourceRoots: [
|
||||||
|
@ -23,27 +25,19 @@ class ErikrafTDropViewProvider {
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
webviewView.webview.html = this.getWebviewContent(webviewView.webview);
|
// Adicionar listener para mensagens do webview
|
||||||
// Listen for messages from the webview
|
webviewView.webview.onDidReceiveMessage(
|
||||||
handleWebviewMessages(webviewView.webview);
|
message => {
|
||||||
}
|
if (message.command === 'openExternal') {
|
||||||
}
|
vscode.env.openExternal(vscode.Uri.parse(message.url));
|
||||||
|
|
||||||
// Função para lidar com mensagens do webview
|
|
||||||
function handleWebviewMessages(webview) {
|
|
||||||
webview.onDidReceiveMessage(async (message) => {
|
|
||||||
if (message && message.type && message.url) {
|
|
||||||
if (message.type === 'external-link' || message.type === 'download') {
|
|
||||||
try {
|
|
||||||
await vscode.env.openExternal(vscode.Uri.parse(message.url));
|
|
||||||
} catch (err) {
|
|
||||||
vscode.window.showErrorMessage('Não foi possível abrir o link externo: ' + message.url);
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
null,
|
||||||
});
|
context.subscriptions
|
||||||
}
|
);
|
||||||
|
|
||||||
|
webviewView.webview.html = this.getWebviewContent(webviewView.webview);
|
||||||
|
}
|
||||||
|
|
||||||
getWebviewContent(webview) {
|
getWebviewContent(webview) {
|
||||||
return `
|
return `
|
||||||
|
@ -51,7 +45,7 @@ function handleWebviewMessages(webview) {
|
||||||
<html lang="pt-br">
|
<html lang="pt-br">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src ${webview.cspSource} https:; style-src ${webview.cspSource}; script-src ${webview.cspSource}; frame-src https://drop.erikraft.com;">
|
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src ${webview.cspSource} https:; style-src ${webview.cspSource} 'unsafe-inline'; script-src ${webview.cspSource} 'unsafe-inline'; frame-src https://drop.erikraft.com/">
|
||||||
<title>ErikrafT Drop</title>
|
<title>ErikrafT Drop</title>
|
||||||
<style>
|
<style>
|
||||||
html, body {
|
html, body {
|
||||||
|
@ -82,33 +76,79 @@ function handleWebviewMessages(webview) {
|
||||||
height="844"
|
height="844"
|
||||||
style="border: none; border-radius: 16px;"
|
style="border: none; border-radius: 16px;"
|
||||||
allow="clipboard-write; camera; microphone; autoplay;"
|
allow="clipboard-write; camera; microphone; autoplay;"
|
||||||
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-downloads"
|
sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-popups-to-escape-sandbox"
|
||||||
referrerpolicy="strict-origin-when-cross-origin"
|
|
||||||
></iframe>
|
></iframe>
|
||||||
<script>
|
<script>
|
||||||
const vscode = acquireVsCodeApi();
|
(function() {
|
||||||
|
const vscode = acquireVsCodeApi();
|
||||||
|
const iframe = document.querySelector('iframe');
|
||||||
|
|
||||||
// Listen for messages coming from any embedded iframe (cross-origin)
|
function setupLinkHandler() {
|
||||||
window.addEventListener('message', (event) => {
|
try {
|
||||||
if (event.data && (event.data.type === 'external-link' || event.data.type === 'download')) {
|
const iframeWindow = iframe.contentWindow;
|
||||||
vscode.postMessage(event.data);
|
const iframeDocument = iframeWindow.document;
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Intercept clicks inside the webview itself
|
// Função para abrir link externo
|
||||||
document.addEventListener('click', (event) => {
|
function openExternalLink(url) {
|
||||||
const link = event.target.closest('a');
|
vscode.postMessage({
|
||||||
if (link && link.href.startsWith('http')) {
|
command: 'openExternal',
|
||||||
event.preventDefault();
|
url: url
|
||||||
vscode.postMessage({ type: 'external-link', url: link.href });
|
});
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
// Sobrescrever o comportamento padrão de links
|
||||||
|
iframeWindow.addEventListener('click', function(e) {
|
||||||
|
const link = e.target.closest('a');
|
||||||
|
if (link && link.href && link.href.startsWith('http')) {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
openExternalLink(link.href);
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
// Sobrescrever window.open
|
||||||
|
iframeWindow.open = function(url) {
|
||||||
|
if (url && url.startsWith('http')) {
|
||||||
|
openExternalLink(url);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return window.open.apply(this, arguments);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sobrescrever location.href
|
||||||
|
Object.defineProperty(iframeWindow.location, 'href', {
|
||||||
|
set: function(url) {
|
||||||
|
if (url && url.startsWith('http')) {
|
||||||
|
openExternalLink(url);
|
||||||
|
} else {
|
||||||
|
iframeWindow.location = url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao configurar handler de links:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const downloadLink = event.target.closest('a[download]');
|
|
||||||
if (downloadLink) {
|
// Tentar configurar o handler quando o iframe carregar
|
||||||
event.preventDefault();
|
iframe.addEventListener('load', function() {
|
||||||
vscode.postMessage({ type: 'download', url: downloadLink.href });
|
setupLinkHandler();
|
||||||
}
|
// Tentar novamente após um delay para garantir que o conteúdo esteja carregado
|
||||||
});
|
setTimeout(setupLinkHandler, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Backup: tentar configurar periodicamente
|
||||||
|
let attempts = 0;
|
||||||
|
const interval = setInterval(function() {
|
||||||
|
if (attempts < 5) {
|
||||||
|
setupLinkHandler();
|
||||||
|
attempts++;
|
||||||
|
} else {
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue