-
Notifications
You must be signed in to change notification settings - Fork 7
/
bookmarklet.js
126 lines (111 loc) · 5.88 KB
/
bookmarklet.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
javascript: (
function () {
const pollInterval = 69;
const resultWaitTime = 420;
const defaultIndentation = 4;
if (!document.getElementById('generate-button')) {
console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function (msg) {
/* 'current: true' is logged when download checkbox is clicked, ignore it*/
if (!JSON.stringify(msg).includes('current:')) {
console.logs.push(Array.from(arguments));
console.stdlog.apply(console, arguments);
}
};
const pollForElement = (elem, timeout, callback) => {
const intervalPoll = setInterval(() => {
const element = document.querySelector(elem);
if (element) {
clearInterval(intervalPoll);
callback();
};
}, pollInterval);
setTimeout(() => clearInterval(intervalPoll), timeout);
};
/* https://stackoverflow.com/questions/33855641/copy-output-of-a-javascript-variable-to-the-clipboard */
const copyToClipboard = (text) => {
const dummy = document.createElement('textarea');
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand('copy');
document.body.removeChild(dummy);
};
/* https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser */
const downloadFile = (text) => {
if (document.getElementById('download-checkbox').checked) {
const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(text);
const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute('href', dataStr);
downloadAnchorNode.setAttribute('download', 'download.json');
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
};
const onClick = () => {
if (!document.body.classList.contains('loading')) {
console.logs = [];
document.getElementById('submit-button').click();
pollForElement('.summary', 3000, () => {
setTimeout(() => {
const queryLog = console.logs[1];
const resultLog = console.logs[2];
console.logs = [];
const query = queryLog[0];
const result = resultLog && resultLog[1];
let obj;
if (result) {
obj = {
title: 'generated by https://github.com/ianmah/310-bookmarklet. Pls gib star',
query,
isQueryValid: true,
...result
}
} else {
const errorMessages = document.getElementsByClassName('error-message');
const errorMessage = errorMessages.length && errorMessages[0].textContent.trim().replace('Unexpected response status 400: ', '');
/* In the case that the button was spammed, there will be no result and no error message, do nothing.
There should already be a valid test case in the clipboard by then */
if (errorMessage) {
const errorType = (errorMessage.includes('The result is too big. Only queries with a maximum of 5000 results are supported.')) ? 'ResultTooLargeError' : 'InsightError';
obj = {
title: `${errorMessage}. Generated by https://github.com/ianmah/310-bookmarklet. Pls gib star`,
query,
isQueryValid: false,
result: errorType
};
}
}
/* Do nothing if there is no object */
const stringifiedObj = JSON.stringify(obj, null, defaultIndentation);
if (obj) {
copyToClipboard(stringifiedObj);
downloadFile(stringifiedObj);
};
console.logs = [];
}, resultWaitTime);
});
};
};
const parent = document.getElementById('submit-container');
const button = document.createElement('a');
const lineBreak = document.createElement('br');
const downloadCheckboxLabel = document.createElement('span');
const downloadCheckbox = document.createElement('input');
downloadCheckboxLabel.innerHTML = 'Download Test File';
button.innerHTML = 'Copy Test File';
button.id = 'generate-button';
button.className = 'btn';
button.href = '#!';
button.onclick = onClick;
downloadCheckbox.type = 'checkbox';
downloadCheckbox.id = 'download-checkbox';
parent.appendChild(button);
parent.appendChild(lineBreak);
parent.appendChild(downloadCheckboxLabel);
parent.appendChild(downloadCheckbox);
}
}
)();