Skip to content

Commit eaa5765

Browse files
author
hoang.tran12
committed
file saver - WIP
1 parent 4ddf80f commit eaa5765

File tree

4 files changed

+217
-18
lines changed

4 files changed

+217
-18
lines changed

scripts/_test.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ export default {
88
en: "",
99
vi: "",
1010
},
11-
whiteList: ["https://graph.facebook.com/*"],
1211

1312
onClick: async () => {
14-
let ACCESS_TOKEN = prompt("Nhập access token của bạn vào đây");
15-
if (!ACCESS_TOKEN) return;
16-
17-
let id = prompt("Nhập ID của user, group, page cần lấy group id", "");
18-
if (!id) return;
19-
20-
alert("Xem kết quả trong console");
21-
fetch(
22-
`https://graph.facebook.com/v13.0/${id}/albums?fields=type,name,count,link,created_time&limit=100&access_token=${ACCESS_TOKEN}`
23-
)
24-
.then((res) => res.json())
25-
.then((json) => {
26-
console.log(json.data);
27-
})
28-
.catch((err) => {
29-
console.log(err);
30-
});
13+
try {
14+
let abc = getEventListeners(
15+
document.querySelector("#wheelCanvas")
16+
).click[0].listener.toString();
17+
console.log(abc);
18+
} catch (e) {
19+
console.error(e);
20+
}
3121
},
3222
};

scripts/libs/file-saver/index.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// prettier-ignore
2+
(function (global, factory) {
3+
if (typeof define === "function" && define.amd) {
4+
define([], factory);
5+
} else if (typeof exports !== "undefined") {
6+
factory();
7+
} else {
8+
var mod = {
9+
exports: {}
10+
};
11+
factory();
12+
global.FileSaver = mod.exports;
13+
}
14+
})(this, function () {
15+
"use strict";
16+
17+
/*
18+
* FileSaver.js
19+
* A saveAs() FileSaver implementation.
20+
*
21+
* By Eli Grey, http://eligrey.com
22+
*
23+
* License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
24+
* source : http://purl.eligrey.com/github/FileSaver.js
25+
*/
26+
// The one and only way of getting global scope in all environments
27+
// https://stackoverflow.com/q/3277182/1008999
28+
var _global = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
29+
30+
function bom(blob, opts) {
31+
if (typeof opts === 'undefined') opts = {
32+
autoBom: false
33+
};else if (typeof opts !== 'object') {
34+
console.warn('Deprecated: Expected third argument to be a object');
35+
opts = {
36+
autoBom: !opts
37+
};
38+
} // prepend BOM for UTF-8 XML and text/* types (including HTML)
39+
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
40+
41+
if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
42+
return new Blob([String.fromCharCode(0xFEFF), blob], {
43+
type: blob.type
44+
});
45+
}
46+
47+
return blob;
48+
}
49+
50+
function download(url, name, opts) {
51+
var xhr = new XMLHttpRequest();
52+
xhr.open('GET', url);
53+
xhr.responseType = 'blob';
54+
55+
let startTime = Date.now();
56+
57+
xhr.onload = function () {
58+
saveAs(xhr.response, name, opts);
59+
};
60+
61+
xhr.onprogress = function (e) {
62+
if (typeof opts.onprogress === 'function') {
63+
opts.onprogress({
64+
loaded: e.loaded,
65+
total: e.total,
66+
percent: e.loaded / e.total * 100,
67+
speed: e.loaded / (Date.now() - startTime),
68+
e
69+
});
70+
}
71+
};
72+
73+
xhr.onerror = function () {
74+
console.error('could not download file');
75+
};
76+
77+
xhr.send();
78+
}
79+
80+
function corsEnabled(url) {
81+
var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
82+
83+
xhr.open('HEAD', url, false);
84+
85+
try {
86+
xhr.send();
87+
} catch (e) {}
88+
89+
return xhr.status >= 200 && xhr.status <= 299;
90+
} // `a.click()` doesn't work for all browsers (#465)
91+
92+
93+
function click(node) {
94+
try {
95+
node.dispatchEvent(new MouseEvent('click'));
96+
} catch (e) {
97+
var evt = document.createEvent('MouseEvents');
98+
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
99+
node.dispatchEvent(evt);
100+
}
101+
} // Detect WebView inside a native macOS app by ruling out all browsers
102+
// We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
103+
// https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
104+
105+
106+
var isMacOSWebView = /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
107+
var saveAs = _global.saveAs || ( // probably in some web worker
108+
typeof window !== 'object' || window !== _global ? function saveAs() {}
109+
/* noop */
110+
// Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
111+
: 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
112+
var URL = _global.URL || _global.webkitURL;
113+
var a = document.createElement('a');
114+
name = name || blob.name || 'download';
115+
a.download = name;
116+
a.rel = 'noopener'; // tabnabbing
117+
// TODO: detect chrome extensions & packaged apps
118+
// a.target = '_blank'
119+
120+
if (typeof blob === 'string') {
121+
// Support regular links
122+
a.href = blob;
123+
124+
if (a.origin !== location.origin) {
125+
corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
126+
} else {
127+
click(a);
128+
}
129+
} else {
130+
// Support blobs
131+
a.href = URL.createObjectURL(blob);
132+
setTimeout(function () {
133+
URL.revokeObjectURL(a.href);
134+
}, 4E4); // 40s
135+
136+
setTimeout(function () {
137+
click(a);
138+
}, 0);
139+
}
140+
} // Use msSaveOrOpenBlob as a second approach
141+
: 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
142+
name = name || blob.name || 'download';
143+
144+
if (typeof blob === 'string') {
145+
if (corsEnabled(blob)) {
146+
download(blob, name, opts);
147+
} else {
148+
var a = document.createElement('a');
149+
a.href = blob;
150+
a.target = '_blank';
151+
setTimeout(function () {
152+
click(a);
153+
});
154+
}
155+
} else {
156+
navigator.msSaveOrOpenBlob(bom(blob, opts), name);
157+
}
158+
} // Fallback to using FileReader and a popup
159+
: function saveAs(blob, name, opts, popup) {
160+
// Open a popup immediately do go around popup blocker
161+
// Mostly only available on user interaction and the fileReader is async so...
162+
popup = popup || open('', '_blank');
163+
164+
if (popup) {
165+
popup.document.title = popup.document.body.innerText = 'downloading...';
166+
}
167+
168+
if (typeof blob === 'string') return download(blob, name, opts);
169+
var force = blob.type === 'application/octet-stream';
170+
171+
var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
172+
173+
var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
174+
175+
if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
176+
// Safari doesn't allow downloading of blob URLs
177+
var reader = new FileReader();
178+
179+
reader.onloadend = function () {
180+
var url = reader.result;
181+
url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
182+
if (popup) popup.location.href = url;else location = url;
183+
popup = null; // reverse-tabnabbing #460
184+
};
185+
186+
reader.readAsDataURL(blob);
187+
} else {
188+
var URL = _global.URL || _global.webkitURL;
189+
var url = URL.createObjectURL(blob);
190+
if (popup) popup.location = url;else location.href = url;
191+
popup = null; // reverse-tabnabbing #460
192+
193+
setTimeout(function () {
194+
URL.revokeObjectURL(url);
195+
}, 4E4); // 40s
196+
}
197+
});
198+
_global.saveAs = saveAs.saveAs = saveAs;
199+
200+
if (typeof module !== 'undefined') {
201+
module.exports = saveAs;
202+
}
203+
});

scripts/youtube_localDownloader.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Useful Script - YouTube Local Downloader</title>
88

9+
<script src="./libs/file-saver/index.js"></script>
910
<script src="./content-scripts/scripts/ufs_global_webpage_context.js"></script>
1011
</head>
1112

scripts/youtube_localDownloader_main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ window.onload = () => {
9191
let data = xhrDownloadUint8Array(video, (progress) => {
9292
console.log(progress);
9393
});
94+
// saveAs(video.url, "video.mp4", {
95+
// onprogress: (e) => {
96+
// console.log(e);
97+
// },
98+
// });
9499
};
95100
document.body.appendChild(button);
96101
}

0 commit comments

Comments
 (0)