-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuserscript-file-explorer.user.js
More file actions
73 lines (68 loc) · 2.4 KB
/
Copy pathuserscript-file-explorer.user.js
File metadata and controls
73 lines (68 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ==UserScript==
// @name UserScript File Explorer
// @namespace http://tampermonkey.net/
// @version 1.0
// @description A script to navigate local directories and open files in new tabs.
// @icon https://i.ibb.co/VxRTZ4K/monky.png
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Create the explorer container
const explorerContainer = document.createElement('div');
explorerContainer.id = 'explorerContainer';
explorerContainer.style.position = 'fixed';
explorerContainer.style.top = '10px';
explorerContainer.style.right = '10px';
explorerContainer.style.width = '300px';
explorerContainer.style.height = '400px';
explorerContainer.style.overflow = 'auto';
explorerContainer.style.backgroundColor = '#fff';
explorerContainer.style.border = '1px solid #ccc';
explorerContainer.style.zIndex = 10000;
explorerContainer.style.padding = '10px';
explorerContainer.style.boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.1)';
document.body.appendChild(explorerContainer);
// Function to fetch directory content
function fetchDirectoryContent(path) {
fetch(`file://${path}`)
.then(response => response.json())
.then(data => {
renderDirectoryContent(data, path);
})
.catch(error => {
console.error('Error fetching directory content:', error);
});
}
// Function to render directory content
function renderDirectoryContent(data, path) {
explorerContainer.innerHTML = '';
if (path !== '/') {
let upLink = document.createElement('div');
upLink.textContent = '..';
upLink.style.cursor = 'pointer';
upLink.onclick = function () {
let parentPath = path.substring(0, path.lastIndexOf('/')) || '/';
fetchDirectoryContent(parentPath);
};
explorerContainer.appendChild(upLink);
}
data.forEach(item => {
let itemElement = document.createElement('div');
itemElement.textContent = item.name;
itemElement.style.cursor = 'pointer';
itemElement.onclick = function () {
if (item.isDirectory) {
fetchDirectoryContent(`${path}/${item.name}`);
} else {
window.open(`file://${path}/${item.name}`, '_blank');
}
};
explorerContainer.appendChild(itemElement);
});
}
// Initial fetch for the root directory
fetchDirectoryContent('/');
})();