-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.user.js
281 lines (242 loc) · 8.46 KB
/
script.user.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
// ==UserScript==
// @name Google Forms Unlocker
// @namespace https://github.com/xNasuni/google-forms-unlocker
// @description Stops Google Forms from being locked, consequently letting you do them without a chromebook.
// @author Mia @ github.com/xNasuni
// @match *://docs.google.com/forms/*
// @grant GM_addStyle
// @version 1.7
// @run-at document-start
// @updateURL https://github.com/xNasuni/google-forms-unlocker/raw/main/script.user.js
// @downloadURL https://github.com/xNasuni/google-forms-unlocker/raw/main/script.user.js
// @supportURL https://github.com/xNasuni/google-forms-unlocker/issues
// ==/UserScript==
const kAssessmentAssistantExtensionId = "gndmhdcefbhlchkhipcnnbkcmicncehk"
const ERROR_USER_AGENT = "_useragenterror"
const ERROR_UNKNOWN = "_unknown"
var shouldSpoof = location.hash === "#gfu"
// support for browsers other than chrome.
unsafeWindow.chrome = unsafeWindow.chrome || {}
unsafeWindow.chrome.runtime = unsafeWindow.chrome.runtime || {}
unsafeWindow.chrome.runtime.sendMessage = unsafeWindow.chrome.runtime.sendMessage || function(extId, payload, callback){chrome.runtime.lastError = 1; callback()}
const oldSendMessage = unsafeWindow.chrome.runtime.sendMessage
if (GM_addStyle === undefined) {
// https://stackoverflow.com/questions/23683439/gm-addstyle-equivalent-in-tampermonkey
GM_addStyle = function (css) {
const style = unsafeWindow.document.getElementById("GM_addStyleBy8626") || (function () {
const style = unsafeWindow.document.createElement('style');
style.type = 'text/css';
style.id = "GM_addStyleBy8626";
unsafeWindow.document.head.appendChild(style);
return style;
})();
const sheet = style.sheet;
sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
}
}
function ButtonAction() {
location.hash = "gfu"
location.reload()
}
function MatchExtensionId(ExtensionId) {
return ExtensionId === kAssessmentAssistantExtensionId
}
function GetGoogleForm() {
const Containers = unsafeWindow.document.querySelectorAll("div.RGiwf")
var Form
for (const Container of Containers) {
for (const Child of Container.childNodes) {
if (Child.nodeName == "FORM") {
Form = Child
}
}
}
return Form
}
function GetQuizHeader() {
const QuizHeader = unsafeWindow.document.querySelector("div.mGzJpd")
return QuizHeader
}
function PageIsErrored() {
const QuizHeader = GetQuizHeader()
if (QuizHeader === null) { return false }
const ChildNodes = QuizHeader.childNodes
if (ChildNodes[3].getAttribute("aria-live") === "assertive" && ChildNodes[4].getAttribute("aria-live") === "assertive") {
return {title: ChildNodes[3].innerText, description: ChildNodes[4].innerText}
}
return false
}
function MatchErrorType(error) {
if (error.title === "You can't access this quiz." && error.description === "Locked mode is on. Only respondents using managed Chromebooks can open this quiz. Learn more") {
return ERROR_USER_AGENT
}
return ERROR_UNKNOWN
}
function MakeButton(Text, Callback, Color) {
const Form = GetGoogleForm()
if (Form === undefined) { return false }
const ButtonHolder = Form.childNodes[2]
const Button = unsafeWindow.document.createElement("div")
Button.classList.value = "uArJ5e UQuaGc Y5sE8d TIHcue QvWxOd"
Button.style.marginLeft = "10px"
Button.style.backgroundColor = Color
Button.setAttribute("role", "button")
Button.setAttribute("tabindex", ButtonHolder.childNodes.length)
Button.setAttribute("mia-gfu-state", "custom-button")
ButtonHolder.appendChild(Button)
const Glow = unsafeWindow.document.createElement("div")
Glow.classList.value = "Fvio9d MbhUzd"
Glow.style.top = '21px'
Glow.style.left = '9px'
Glow.style.width = '110px'
Glow.style.height = '110px'
Button.appendChild(Glow)
const TextContainer = unsafeWindow.document.createElement("span")
TextContainer.classList.value = "l4V7wb Fxmcue"
Button.appendChild(TextContainer)
const TextSpan = unsafeWindow.document.createElement("span")
TextSpan.classList.value = "NPEfkd RveJvd snByac"
TextSpan.innerText = Text
TextContainer.appendChild(TextSpan)
Button.addEventListener("click", Callback)
return {destroy: function(){Button.remove()}}
}
async function IsOnChromebook() {
return new Promise((resolve, _reject) => {
oldSendMessage(kAssessmentAssistantExtensionId, {command: "isLocked"}, function(_response) {
if (unsafeWindow.chrome.runtime.lastError) {
resolve(false)
}
resolve(true)
})
})
}
async function Initialize() {
GM_addStyle(`
.gfu-red {
font-family: monospace;
text-align: center;
font-size: 11px;
padding-top: 24px;
color: red !important;
}
.EbMsme {
transition: filter cubic-bezier(0.4, 0, 0.2, 1) 0.3s;
filter: blur(8px) !important;
}
.EbMsme:hover {
filter: blur(0px) !important;
}
`)
const Errored = PageIsErrored()
if (Errored !== false) {
switch (MatchErrorType(Errored)) {
case ERROR_USER_AGENT:
const QuizHeader = GetQuizHeader()
const Error = unsafeWindow.document.createElement("div")
Error.classList.value = "gfu-red"
QuizHeader.appendChild(Error)
const ErrorSpan = unsafeWindow.document.createElement("span")
ErrorSpan.innerText = "Google Forms Unlocker - In order to continue, you need a User Agent Spoofer. "
Error.appendChild(ErrorSpan)
const AnchorSpan = unsafeWindow.document.createElement("a")
AnchorSpan.classList.value = "gfu-red"
AnchorSpan.innerText = "Install one here."
AnchorSpan.target = "_blank"
AnchorSpan.rel = "noopener"
AnchorSpan.href = "https://github.com/xNasuni/google-forms-unlocker/blob/main/README.md#spoofing-your-user-agent"
ErrorSpan.appendChild(AnchorSpan)
break
default:
alert(`Unhandled error type: ${JSON.stringify(Errored)}`)
}
return
}
const Form = GetGoogleForm()
if (Form === undefined) { return false }
const IsRealManagedChromebook = await IsOnChromebook()
if (IsRealManagedChromebook === false) {
const ButtonHolder = Form.childNodes[2]
for (const Button of ButtonHolder.childNodes) {
if (Button.getAttribute("mia-gfu-state") === "custom-button") { continue }
Button.style.backgroundColor = "#ccc"
Button.setAttribute("jsaction", "")
}
}
MakeButton("Bypass", ButtonAction, "#ff90bf")
}
var fakeIsLocked = shouldSpoof
function InterceptCommand(Payload, Callback) {
switch (Payload.command) {
case "isLocked":
Callback({locked: fakeIsLocked})
return true
case "lock":
if (shouldSpoof) {
return false
}
fakeIsLocked = false
Callback({locked: fakeIsLocked})
return true
case "unlock":
fakeIsLocked = false
Callback({locked: fakeIsLocked})
return true
}
return false
}
setInterval(() => {
unsafeWindow.chrome.runtime.sendMessage = function() {
const ExtensionId = (arguments)[0]
const Payload = (arguments)[1]
const Callback = (arguments)[2]
if (MatchExtensionId(ExtensionId)) {
const Intercepted = InterceptCommand(Payload, Callback)
if (Intercepted) { return null }
}
console.warn("Not intercepting", ExtensionId, Payload, Callback)
return oldSendMessage(ExtensionId, Payload, function() {
if (unsafeWindow.chrome.runtime.lastError) {
alert(`Google Forms Unlocker, please report this to the GitHub https://github.com/xNasuni/google-forms-unlocker/issues\nUnhandled error: ${JSON.stringify(chrome.runtime.lastError)}`)
return
}
Callback.apply(this, arguments)
})
}
})
unsafeWindow.document.addEventListener("DOMContentLoaded", () => {
unsafeWindow.console.log("Initialized")
Initialize()
})
Object.defineProperty(unsafeWindow.document, 'hidden', {
value: false,
writable: false
})
Object.defineProperty(unsafeWindow.document, 'visibilityState', {
value: "visible",
writable: false
})
Object.defineProperty(unsafeWindow.document, 'webkitVisibilityState', {
value: "visible",
writable: false
})
Object.defineProperty(unsafeWindow.document, 'mozVisibilityState', {
value: "visible",
writable: false
})
Object.defineProperty(unsafeWindow.document, 'msVisibilityState', {
value: "visible",
writable: false
})
const BlacklistedEvents = ['mozvisibilitychange', 'webkitvisibilitychange', 'msvisibilitychange', 'visibilitychange']
const oldAddEventListener = unsafeWindow.document.addEventListener;
unsafeWindow.document.addEventListener = function() {
const EventType = (arguments)[0]
const Method = (arguments)[1]
const Options = (arguments)[2]
if (BlacklistedEvents.indexOf(EventType) !== -1) {
console.log(`type ${EventType} blocked from being registered with`, Method)
return
}
return oldAddEventListener.apply(this, arguments)
}