forked from ianmah/310-bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarklet.js
91 lines (81 loc) · 4.24 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
javascript:(
function() {
const pollInterval = 69;
const resultWaitTime = 420;
if (!document.getElementById("generate-button")) {
console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function() {
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);
};
const onClick = () => {
if (!document.body.classList.contains('loading')) {
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;
/* 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("Unexpected response status 400: The result is too big. Only queries with a maximum of 5000 results are supported.")) ? "ResultTooLargeError" : "InsightError";
obj = {
title: `${errorMessage.trim().replace("Unexpected response status 400: ", "")}. Generated by https://github.com/ianmah/310-bookmarklet. Pls gib star`,
query,
isQueryValid: false,
result: errorType
};
}
}
/* Do nothing if there is no object */
obj && copyToClipboard(JSON.stringify(obj, null, 4));
console.logs = [];
}, resultWaitTime);
});
};
};
const parent = document.getElementById("submit-container");
const button = document.createElement("a");
button.innerHTML = "Copy Test File";
button.id = "generate-button";
button.className = "btn";
button.href = "#!";
button.onclick = onClick;
parent.appendChild(button);
}
}
)();