-
Notifications
You must be signed in to change notification settings - Fork 0
/
getHtmlOptions.js
55 lines (45 loc) · 1.51 KB
/
getHtmlOptions.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
// ==UserScript==
// @name Get HTML <select> options list
// @author wesinator
// @version 1
// @grant none
// @match *://*/*
// ==/UserScript==
var selects = document.getElementsByTagName("select");
if (selects.length) {
for (var select of selects) {
//if (select.id) {
var getSelects = confirm(`Do you want to download the page's option list "${select.name}" to a text file?`);
var getSelects = confirm(`Do you want to download the page's option list "${select.name || select.id}" to a text file?`);
if (getSelects) {
items = getSelectOptionItems(select);
console.log(items.length, "items")
if (items.length) {
var filename = `options_${document.location.hostname}_${select.id || select.name}.txt`;
arrayToFile(items, filename);
}
}
//}
}
}
function getSelectOptionItems(select) {
var items = [];
for (var item of select) {
if (item) {
console.log(item.text);
items.push(item.text);
}
}
return items;
}
function arrayToFile(list, filename) {
var contents = `/* retrieved from: ${document.URL} \n${Date()} */\n\n` + list.join('\n');
fileDataDownload(contents, filename, "text/plain");
}
function fileDataDownload(contents, name, mimeType) {
// https://stackoverflow.com/questions/34101871/save-data-using-greasemonkey-tampermonkey-for-later-retrieval
var a = document.createElement("a");
a.href = `data:${mimeType};charset=utf-8,` + encodeURIComponent(contents);
a.download = name;
a.click();
}