Files
luxtools-plugin/script.js

59 lines
1.5 KiB
JavaScript

/* global window, document */
(function () {
'use strict';
function normalizeToFileUrl(path) {
if (!path) return '';
// already a file URL
if (/^file:\/\//i.test(path)) return path;
// UNC path: \\server\share\path
if (/^\\\\/.test(path)) {
var p = path.replace(/^\\\\/, '');
p = p.replace(/\\/g, '/');
return 'file://///' + p;
}
// Windows drive: C:\path\to\file
if (/^[a-zA-Z]:\\/.test(path)) {
var drive = path[0].toUpperCase();
var rest = path.slice(2).replace(/\\/g, '/');
return 'file:///' + drive + ':' + rest;
}
// POSIX absolute: /home/user/file
if (path[0] === '/') {
return 'file://' + path;
}
// Fall back to using the provided string.
return path;
}
function onClick(event) {
var el = event.target;
if (!el || !el.classList || !el.classList.contains('filetools-open')) return;
var raw = el.getAttribute('data-path') || '';
var url = normalizeToFileUrl(raw);
console.log('Opening file URL:', url);
if (!url) return;
// Best-effort: browsers may block file:// navigation depending on settings.
try {
window.open(url, '_blank', 'noopener');
} catch (e) {
console.error('Failed to open file URL in new tab:', e);
try {
window.location.href = url;
} catch (e2) {
console.error('Failed to open file URL:', e2);
}
}
}
document.addEventListener('click', onClick, false);
})();