-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
171 lines (143 loc) · 4.2 KB
/
Copy pathcontent.js
File metadata and controls
171 lines (143 loc) · 4.2 KB
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
(() => {
if (window.__noReloadPageInstalled) {
return;
}
window.__noReloadPageInstalled = true;
let enabled = false;
let stopTimer = null;
const startNavigationStopper = () => {
window.stop();
if (stopTimer) {
window.clearInterval(stopTimer);
}
const startedAt = Date.now();
stopTimer = window.setInterval(() => {
window.stop();
for (const type of ["keydown", "keyup"]) {
document.dispatchEvent(
new KeyboardEvent(type, {
key: "Escape",
code: "Escape",
keyCode: 27,
which: 27,
bubbles: true,
cancelable: true
})
);
}
if (Date.now() - startedAt > 5000) {
window.clearInterval(stopTimer);
stopTimer = null;
}
}, 50);
};
const isSubmitControl = (element) => {
if (!(element instanceof HTMLElement)) {
return false;
}
const control = element.closest("button, input");
if (!control) {
return false;
}
const tag = control.tagName.toLowerCase();
const type = (control.getAttribute("type") || "").toLowerCase();
return (
(tag === "button" && (type === "" || type === "submit")) ||
(tag === "input" && (type === "submit" || type === "image"))
);
};
const getFormMethod = (form, submitter) => {
return (
submitter?.getAttribute("formmethod") ||
form.getAttribute("method") ||
"get"
).toUpperCase();
};
const getFormAction = (form, submitter) => {
return new URL(
submitter?.getAttribute("formaction") || form.getAttribute("action") || window.location.href,
window.location.href
);
};
const getFormEncoding = (form, submitter) => {
return (
submitter?.getAttribute("formenctype") ||
form.getAttribute("enctype") ||
"application/x-www-form-urlencoded"
).toLowerCase();
};
const appendSubmitterValue = (formData, submitter) => {
if (!submitter?.name || submitter.disabled) {
return;
}
if (submitter.type === "image") {
formData.append(`${submitter.name}.x`, "0");
formData.append(`${submitter.name}.y`, "0");
return;
}
formData.append(submitter.name, submitter.value);
};
const createPlainTextBody = (formData) => {
return Array.from(formData.entries())
.map(([key, value]) => `${key}=${value instanceof File ? value.name : value}`)
.join("\r\n");
};
const submitWithoutNavigation = async (form, submitter) => {
const method = getFormMethod(form, submitter);
const action = getFormAction(form, submitter);
const encoding = getFormEncoding(form, submitter);
const formData = new FormData(form);
appendSubmitterValue(formData, submitter);
const init = {
method,
credentials: "include",
redirect: "follow"
};
if (method === "GET") {
action.search = new URLSearchParams(formData).toString();
} else if (encoding === "multipart/form-data") {
init.body = formData;
} else if (encoding === "text/plain") {
init.body = createPlainTextBody(formData);
init.headers = { "Content-Type": "text/plain;charset=UTF-8" };
} else {
init.body = new URLSearchParams(formData);
init.headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
};
}
await fetch(action.toString(), init);
};
const handleSubmit = (event) => {
if (!enabled) {
return;
}
startNavigationStopper();
event.preventDefault();
submitWithoutNavigation(event.target, event.submitter).catch((error) => {
console.warn("StayPut could not submit the form.", error);
});
};
document.addEventListener(
"click",
(event) => {
if (!enabled || !isSubmitControl(event.target)) {
return;
}
startNavigationStopper();
},
true
);
document.addEventListener("submit", handleSubmit);
window.addEventListener("beforeunload", () => {
if (enabled) {
startNavigationStopper();
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type === "SET_ENABLED") {
enabled = Boolean(message.enabled);
sendResponse({ enabled });
}
});
})();