Skip to content

Commit d499ab8

Browse files
committed
Create gm_add_element.js
1 parent a9a8a92 commit d499ab8

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

example/tests/gm_add_element.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// ==UserScript==
2+
// @name GM_addElement test
3+
// @match *://*/*?test_GM_addElement
4+
// @grant GM_addElement
5+
// @version 0
6+
// ==/UserScript==
7+
8+
/*
9+
### Example Sites
10+
* https://content-security-policy.com/?test_GM_addElement (CSP)
11+
* https://github.com/scriptscat/scriptcat/?test_GM_addElement (CSP)
12+
* https://www.youtube.com/account_playback/?test_GM_addElement (TTP)
13+
*/
14+
15+
const logSection = (title) => {
16+
console.log(`\n=== ${title} ===`);
17+
};
18+
19+
const logStep = (message, data) => {
20+
if (data !== undefined) {
21+
console.log(`→ ${message}:`, data);
22+
} else {
23+
console.log(`→ ${message}`);
24+
}
25+
};
26+
27+
28+
// ─────────────────────────────────────────────
29+
// Native textarea insertion
30+
// ─────────────────────────────────────────────
31+
logSection("Native textarea insertion - BEGIN");
32+
33+
const textarea = GM_addElement('textarea', {
34+
native: true,
35+
value: "myText",
36+
});
37+
38+
logStep("Textarea value", textarea.value);
39+
logSection("Native textarea insertion - END");
40+
41+
42+
// ─────────────────────────────────────────────
43+
// Div insertion
44+
// ─────────────────────────────────────────────
45+
logSection("Div insertion - BEGIN");
46+
47+
GM_addElement('div', {
48+
innerHTML: '<div id="test777"></div>',
49+
});
50+
51+
logSection("Div insertion - END");
52+
53+
54+
// ─────────────────────────────────────────────
55+
// Span insertion
56+
// ─────────────────────────────────────────────
57+
logSection("Span insertion - BEGIN");
58+
59+
GM_addElement(document.getElementById("test777"), 'span', {
60+
className: "test777-span",
61+
textContent: 'Hello World!',
62+
});
63+
64+
logStep(
65+
"Span content",
66+
document.querySelector("span.test777-span").textContent
67+
);
68+
69+
logSection("Span insertion - END");
70+
71+
72+
// ─────────────────────────────────────────────
73+
// Image insertion
74+
// ─────────────────────────────────────────────
75+
logSection("Image insertion - BEGIN");
76+
77+
let img;
78+
await new Promise((resolve, reject) => {
79+
img = GM_addElement(document.body, 'img', {
80+
src: 'https://www.tampermonkey.net/favicon.ico',
81+
onload: resolve,
82+
onerror: reject
83+
});
84+
85+
logStep("Image element inserted");
86+
});
87+
88+
logStep("Image loaded");
89+
logSection("Image insertion - END");
90+
91+
92+
// ─────────────────────────────────────────────
93+
// Script insertion
94+
// ─────────────────────────────────────────────
95+
logSection("Script insertion - BEGIN");
96+
97+
GM_addElement(document.body, 'script', {
98+
textContent: "window.myCustomFlag = true; console.log('script run ok');",
99+
}, img);
100+
101+
logStep(
102+
"Script inserted before image",
103+
img.previousSibling?.nodeName === "SCRIPT"
104+
);
105+
106+
logSection("Script insertion - END");

0 commit comments

Comments
 (0)