-
Notifications
You must be signed in to change notification settings - Fork 1
/
Template.js
336 lines (316 loc) · 12.1 KB
/
Template.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// ==UserScript==
// @name Template
// @version 0.2
// @description try to take over the world!
// @include *
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
(async () => {
//Don't touch. This guarantees that any JS object methods used here are not altered by the website.
//Anyone who create a userscript must do this to avoid specific sites like https://www.furaffinity.net from breaking built-in methods and objects.
//
// Wait for documentElement, wait for iframe load event
// credit: https://greasyfork.org/en/discussions/requests/234649-prevent-website-from-overriding-userscript-s-js-methods#comment-482804
function GetIframe() {
let iframeWindow = {}
try {
const iframe = document.createElement('iframe');
iframe.setAttribute("style", "display: none")
const fragment = document.createDocumentFragment();
fragment.appendChild(iframe);
document.body.appendChild(fragment); //tendency to output "null" when "run-at" is set to "document-start".
iframeWindow = iframe.contentWindow;
//iframe.remove();
} catch {
return //return undefined
}
return iframeWindow;
}
let useableIframe = GetIframe()
if (typeof useableIframe == "undefined") {
return
}
const I = useableIframe
//Settings
const Setting_Delay = 1000
//Stuff you don't touch unless you know what you're doing.
let RaceConditionLock = false
//^This prevents concurrent runs of the code as a failsafe.
//no duplicates on the console log
const SetOfURLs = new Set()
//Run code periodically (recommended for dynamic web pages, infinite scrolling)
window.onload = setInterval(MainCode, Setting_Delay)
window.addEventListener("load",function(event) {
setInterval(MainCode, Setting_Delay)
setTimeout(Spawn_UI_Panel, 1000)
},false);
//Variables to have in the memory across page session
let CurrentUTCTime = ""
let TimeDisplayElement = {}
//Load save values
let StorageSaved_Textarea = ""
let StorageSaved_Checkbox = false
let StorageSaved_Select = [
{value:"Option1", visibleText: "Option one", isSelected: true},
{value:"Option2", visibleText: "Option two", isSelected: false},
{value:"Option3", visibleText: "Option three", isSelected: false}
]
let StorageSaved_Select2 = [
{value:"Option1", visibleText: "Option one", isSelected: false},
{value:"Option2", visibleText: "Option two", isSelected: false},
{value:"Option2", visibleText: "Option three", isSelected: false}
]
let StorageSaved_Radio = [
{visibleText: "Radio1", isSelected: true},
{visibleText: "Radio2", isSelected: false},
{visibleText: "Radio3", isSelected: false}
]
LoadValuesFromStorage()
//UI panel
async function Spawn_UI_Panel() {
//Spawn elements
let DivBox = document.createElement("div")
DivBox.setAttribute("style", "position: fixed;bottom: 40px;right: 40px;z-index: 999; background-color: rgba(64, 64, 64, .5); color: #ffffff; border-radius: 30px; padding: 15px;")
//select
let SelectElement = CreateSelectElement(StorageSaved_Select, false)
DivBox.appendChild(SelectElement)
DivBox.appendChild(document.createElement("br"))
//Select, multiple
let SelectElement2 = CreateSelectElement(StorageSaved_Select2, true)
DivBox.appendChild(SelectElement2)
DivBox.appendChild(document.createElement("br"))
//Checkbox
let Label_For_Checkbox = document.createElement("label")
let CheckBoxElement = document.createElement("input")
CheckBoxElement.setAttribute("type", "checkbox")
if (StorageSaved_Checkbox) {
CheckBoxElement.setAttribute("checked", "")
}
CheckBoxElement.addEventListener(
"change",
function () {
StorageSaved_Checkbox = this.checked
SaveValuesToStorage()
}
)
Label_For_Checkbox.appendChild(CheckBoxElement)
Label_For_Checkbox.appendChild(document.createTextNode("Text here"))
DivBox.appendChild(Label_For_Checkbox)
DivBox.appendChild(document.createElement("br"))
//radio
let Div_RadioButtons = CreateRadioList(StorageSaved_Radio, "RadioGroup1", true)
DivBox.appendChild(Div_RadioButtons)
DivBox.appendChild(document.createElement("br"))
//Textarea
let Textarea_test = document.createElement("textarea")
Textarea_test.setAttribute("style", "white-space: pre; overflow-wrap: normal; overflow-x: scroll;")
Textarea_test.appendChild(document.createTextNode(StorageSaved_Textarea)) //setAttribute on value does not work, since value attribute is not supported in this way
Textarea_test.addEventListener(
"input",
function () {
StorageSaved_Textarea = this.value
SaveValuesToStorage()
}
)
DivBox.appendChild(document.createElement("br"))
//Current time (local system)
CurrentUTCTime = ISOString_to_YYYY_MM_DD_HH_MM_SS(new Date(Date.now()).toISOString())
TimeDisplayElement = document.createElement("div")
TimeDisplayElement.setAttribute("style", "font-family: monospace;")
TimeDisplayElement.appendChild(document.createTextNode(CurrentUTCTime))
DivBox.appendChild(TimeDisplayElement)
DivBox.appendChild(Textarea_test)
//Add to document
let HTMLBody = Array.from(document.getElementsByTagName("BODY")).find((Element) => {return true})
let InnerNodeOfHTMLBody = DescendNode(HTMLBody, [0])
if (InnerNodeOfHTMLBody.IsSuccessful) {
document.body.insertBefore(DivBox, HTMLBody.childNodes[0]);
}
}
//MainCode, runs periodically and used to extract page content.
function MainCode() {
if (!RaceConditionLock) {
RaceConditionLock = true
//Code here
//Example: update time display
CurrentUTCTime = ISOString_to_YYYY_MM_DD_HH_MM_SS(new Date(Date.now()).toISOString()) //Update the display each interval
TimeDisplayElement.textContent = CurrentUTCTime
RaceConditionLock = false
}
}
//reused Functions
function ConsoleLoggingURL(URL_String) {
let URL_truncateProof = URL_String.replace(/^http/, "ttp")
if (!SetOfURLs.has(URL_truncateProof)) {
console.log(URL_truncateProof)
SetOfURLs.add(URL_truncateProof)
}
}
//Load/save values from storage
async function LoadValuesFromStorage() {
StorageSaved_Textarea = await GM.getValue("TemplateSaveTest_Textarea", "").catch( () => {
GetValueErrorMessage()
})
StorageSaved_Checkbox = await GM.getValue("TemplateSaveTest_CheckBox", false).catch( () => {
GetValueErrorMessage()
})
StorageSaved_Select = JSON.parse(await GM.getValue("TemplateSaveTest_Select", JSON.stringify(StorageSaved_Select)).catch( () => {
GetValueErrorMessage()
}))
StorageSaved_Select2 = JSON.parse(await GM.getValue("TemplateSaveTest_Select2", JSON.stringify(StorageSaved_Select2)).catch( () => {
GetValueErrorMessage()
}))
StorageSaved_Radio = JSON.parse(await GM.getValue("TemplateSaveTest_Radio", JSON.stringify(StorageSaved_Radio)).catch( () => {
GetValueErrorMessage()
}))
}
function GetValueErrorMessage() {
console.log("TemplateScript - getvalue failed.")
}
async function SaveValuesToStorage() {
await GM.setValue("TemplateSaveTest_Textarea", StorageSaved_Textarea).catch( () => {
SetValueErrorMessage()
})
await GM.setValue("TemplateSaveTest_CheckBox", StorageSaved_Checkbox).catch( () => {
GetValueErrorMessage()
})
await GM.setValue("TemplateSaveTest_Select", JSON.stringify(StorageSaved_Select)).catch( () => {
GetValueErrorMessage()
})
await GM.setValue("TemplateSaveTest_Select2", JSON.stringify(StorageSaved_Select2)).catch( () => {
GetValueErrorMessage()
})
await GM.setValue("TemplateSaveTest_Radio", JSON.stringify(StorageSaved_Radio)).catch( () => {
GetValueErrorMessage()
})
}
function SetValueErrorMessage() {
console.log("TemplateScript - setvalue failed.")
}
//Reused functions
function DescendNode(Node, LevelsArray) {
//Opposite of AscendNode, descends a node without errors. LevelsArray is an array that contains
//only numbers on what child to descend on.
let CurrentNode = Node
if (typeof LevelsArray == "undefined") {
return "Uhh..."
}
let LevelsDown = LevelsArray.length
let ParentCount = 0
for (let i = 0; i < LevelsDown; i++) {
if (typeof CurrentNode.childNodes != "undefined") {
if (typeof CurrentNode.childNodes[LevelsArray[i]] != "undefined") {
CurrentNode = CurrentNode.childNodes[LevelsArray[i]]
ParentCount++
}
} else {
break
}
}
return {
OutputNode: CurrentNode,
LevelsPassed: ParentCount,
IsSuccessful: (ParentCount == LevelsArray.length)
}
}
function AscendNode(Node, Levels) {
//Instead of Node.parentNode.parentNode.parentNode... which is prone to errors if there is no parent, this has a check to prevent it.
//Arguments:
//-Node: The node in the HTML
//-Levels: A number, representing how many levels to ascend
//Will return:
//-the parent node at "Levels" up, unless it cannot go up any further, then the highest
//-the number of successful levels it goes up.
let CurrentNode = Node
let ChildCount = 0
if (typeof Node != "undefined") {
for (let i = 0; i < Levels; i++) {
if (typeof CurrentNode.parentNode != "undefined") {
CurrentNode = CurrentNode.parentNode
ChildCount++
} else {
break
}
}
return {
OutputNode: CurrentNode,
LevelsPassed: ChildCount,
IsSuccessful: (Levels == ChildCount)
}
} else {
return {
OutputNode: undefined,
LevelsPassed: -1,
IsSuccessful: false
}
}
}
function CreateSelectElement(arrayOptions, allowMultiple) {
//Input:
//arrayOptions:
// [{value: "ValueString", visibleText: "Text here", isSelected: false}]
//allowMultiple: false = select only one, true = allow multiple
let HTML_Element_Select = document.createElement("select")
HTML_Element_Select.addEventListener(
"change",
function () {
Array.from(this.childNodes).forEach((listedOption, Index) => {
arrayOptions[Index].isSelected = listedOption.selected
})
SaveValuesToStorage()
}
)
if (allowMultiple) {
HTML_Element_Select.setAttribute("multiple", "")
}
arrayOptions.forEach((listedOption) => {
let HTML_Element_Option = document.createElement("option")
HTML_Element_Option.setAttribute("value", listedOption.value)
if (listedOption.isSelected) {
HTML_Element_Option.setAttribute("selected", "")
}
HTML_Element_Option.appendChild(document.createTextNode(listedOption.visibleText))
HTML_Element_Select.appendChild(HTML_Element_Option)
})
return HTML_Element_Select
}
function CreateRadioList(arrayRadios, elementName, lineBreakEach) {
//Input:
//arrayRadios:
// [{visibleText: "Radio1", isSelected: true}]
//elementName: the name for radio group
let HTML_Element_Div = document.createElement("div")
arrayRadios.forEach((radioItem) => {
let HTML_Element_Label = document.createElement("label")
let HTML_Element_Radio = document.createElement("input")
HTML_Element_Radio.setAttribute("type", "radio")
HTML_Element_Radio.setAttribute("name", "elementName")
HTML_Element_Radio.addEventListener(
"change",
function () {
Array.from(this.parentNode.parentNode.childNodes).filter((ElementListItem) => {
return ElementListItem.tagName == "LABEL"
}).forEach((radioOption, Index) => {
arrayRadios[Index].isSelected = radioOption.childNodes[0].checked
})
SaveValuesToStorage()
}
)
if (radioItem.isSelected) {
HTML_Element_Radio.setAttribute("checked", "")
}
HTML_Element_Label.appendChild(HTML_Element_Radio)
HTML_Element_Label.appendChild(document.createTextNode(radioItem.visibleText))
HTML_Element_Div.appendChild(HTML_Element_Label)
if (lineBreakEach) {
HTML_Element_Div.appendChild(document.createElement("br"))
}
})
return HTML_Element_Div
}
function ISOString_to_YYYY_MM_DD_HH_MM_SS(ISOString) {
//YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ
return ISOString.replace("T", " ").replace(/\.\d{3}Z$/, "") + " UTC"
}
})();