forked from mumawQAQ/14828
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_file.js
74 lines (70 loc) · 2.35 KB
/
local_file.js
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
// ==UserScript==
// @name hijack the click and write to local file
// @namespace http://tampermonkey.net/
// @version 0.1
// @description hijack the click and write to local file
// @author Guangrui Wang
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @require https://code.jquery.com/jquery-latest.js
// @match *://*/*
// ==/UserScript==
/**
* You can copy this script to your tampermonkey and run it on any website
*/
let $ = window.jQuery;
(function () {
'use strict';
$(document).ready(function () {
// hijack the click event
$("html").click(() => {
// use inject script to pass by the sandbox
const injectedScript = document.createElement('script');
// all this part is for the file system access api
injectedScript.textContent = `
(function() {
// Open file action
new Promise((resolve) => resolve(window.showOpenFilePicker()))
.then((handles) => {
const fileHandle = handles[0];
return fileHandle.getFile();
})
.then((file) => {
return file.text();
})
.then((fileContent) => {
document.getElementById('file-content').innerText = fileContent;
})
.catch((error) => {
console.error('Error reading the file:', error);
});
// Save file action
new Promise((resolve) => resolve(window.showSaveFilePicker({
suggestedName: 'example.txt',
types: [
{
description: 'Text Files',
accept: {
'text/plain': ['.txt'],
},
},
],
})))
.then((fileHandle) => {
return fileHandle.createWritable();
})
.then((writableStream) => {
writableStream.write('Hello, File System Access API!');
return writableStream.close();
})
.then(() => {
alert('File saved successfully.');
})
.catch((error) => {
console.error('Error saving the file:', error);
});
})();
`;
document.body.appendChild(injectedScript);
});
});
})();