Открытие ссылок в новой вкладке из веб-браузера Google Chrome App
У меня есть приложение Google Chrome с контролем веб-просмотра. Некоторые ссылки в веб-представлении предназначены для открытия в новой вкладке (target="_blank"
). Однако щелчок по этим ссылкам ничего не делает, а щелчок по ним правой кнопкой мыши не открывает контекстное меню, чтобы открыть / скопировать ссылку. Как я могу включить такие ссылки?
1 ответ
Это лучшее, что я придумал до сих пор:
var webview = null;
function isSafeUrl(url) {
// You may want to perform a more rigorous check.
// There's a technique that creates an <a> to parse the URI, but that seems
// like a security risk.
return !!url.match(/^(?:ftp|https?):\/\//i);
}
function onNewWindow(event) {
if (!isSafeUrl(event.targetUrl)) {
console.warn('Blocking unsafe URL: ' + event.targetUrl);
event.window.discard();
return;
}
var newWindow = null, features = '';
switch (event.windowOpenDisposition) {
case 'ignore':
// Not sure what this is used by. Default enum value, maybe.
console.debug('Ignoring new window request');
return;
case 'save_to_disk':
// Ctrl + S, maybe? Not sure how to reproduce that.
console.log('save_to_disk is not implemented');
return;
case 'current_tab':
webview.src = event.targetUrl;
break;
case 'new_background_tab':
case 'new_foreground_tab':
newWindow = open(event.targetUrl, '_blank');
if (event.windowOpenDisposition != 'new_background_tab') {
newWindow.focus();
}
break;
case 'new_window':
case 'new_popup':
if (event.initialWidth && event.initialHeight) {
features = 'width=' + event.initialWidth + ',height=' + event.initialHeight;
}
newWindow = open(event.targetUrl, '_blank', features);
newWindow.focus();
break;
}
}
function onDomReady() {
webview = document.getElementById('webview');
webview.addEventListener('newwindow', onNewWindow);
}
document.addEventListener('DOMContentLoaded', onDomReady);