-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuserscript-file-picker.user.js
More file actions
80 lines (73 loc) · 2.74 KB
/
Copy pathuserscript-file-picker.user.js
File metadata and controls
80 lines (73 loc) · 2.74 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
74
75
76
77
78
79
// ==UserScript==
// @name UserScript File Picker
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Use the File Picker API to show local directory/file navigator.
// @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);
// File picker UI
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.webkitdirectory = true;
fileInput.multiple = true;
explorerContainer.appendChild(fileInput);
fileInput.addEventListener('change', (event) => {
explorerContainer.innerHTML = '';
for (const file of event.target.files) {
const fileDiv = document.createElement('div');
fileDiv.textContent = file.webkitRelativePath;
fileDiv.style.cursor = 'pointer';
fileDiv.onclick = () => {
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
};
explorerContainer.appendChild(fileDiv);
}
});
// Fetch files from localhost
const fetchButton = document.createElement('button');
fetchButton.textContent = 'Fetch Localhost Folder';
fetchButton.style.display = 'block';
fetchButton.style.marginTop = '10px';
explorerContainer.appendChild(fetchButton);
fetchButton.onclick = function () {
explorerContainer.innerHTML = '<div>Loading from http://localhost:8080/path/to/folder ...</div>';
fetch('http://localhost:8080/path/to/folder')
.then(response => response.json())
.then(data => {
explorerContainer.innerHTML = '';
if (Array.isArray(data)) {
data.forEach(item => {
const fileDiv = document.createElement('div');
fileDiv.textContent = item;
explorerContainer.appendChild(fileDiv);
});
} else {
explorerContainer.innerHTML = '<div>Unexpected response format</div>';
}
})
.catch(err => {
explorerContainer.innerHTML = `<div style="color:red;">Error: ${err}</div>`;
});
};
})();