-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpreview.html
More file actions
198 lines (185 loc) · 6.31 KB
/
preview.html
File metadata and controls
198 lines (185 loc) · 6.31 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Hacklet - A Hack Club Event">
<meta name="author" content="Hack Club" />
<meta property="og:type" content="website" />
<link rel="icon" href="icon.png" type="image/png" />
<link rel="apple-touch-icon" href="icon.png" />
<style>
body {
margin: 0;
padding: 0;
height: 120vh;
background: #000;
color: #fff;
font-family: 'Courier New', Courier, monospace;
}
a {
text-decoration: underline;
color: inherit;
}
#bookmarklet-form {
margin: 20px auto;
text-align: center;
}
#bookmarklet-code {
width: 80%;
padding: 10px;
font-family: 'Courier New', Courier, monospace;
font-size: 1rem;
margin-bottom: 10px;
}
#bookmarklet-form button {
background-color: #000;
color: #fff;
border: 2px solid #fff;
padding: 10px 20px;
cursor: pointer;
font-size: 1.2rem;
transition: background-color 0.3s, color 0.3s;
}
#bookmarklet-form button:hover {
background-color: #fff;
color: #000;
}
#record-name {
margin-left: 8px;
}
pre {
padding: 10px;
}
</style>
</head>
<body>
<center><h2 id="record-name">Loading...</h2></center>
<center>
<span style="font-size: 0.9em; color: gray; display: inline-flex; align-items: center; gap: 5px;">
<p id="record-id"></p>
</span>
</center>
<p id="record-description">Loading...</p>
<center><a href="#" id="test-bookmarklet">Drag me to your bookmarks bar!</a></center>
<form id="bookmarklet-form">
<pre
id="record-code"
style="
width: 80%;
padding: 10px;
font-family: 'Courier New', Courier, monospace;
font-size: 1rem;
margin-bottom: 10px;
text-align: left;
background: white;
color: black;
border: 1px solid #ccc;
overflow: auto;
margin: 0 auto;
"
>
Loading...</pre>
<br />
<button type="button" onclick="testBookmarklet()">Test Bookmarklet</button>
</form>
<script>
// Decode base64-encoded JSON payload
function decodePayload(b64) {
try {
const json = decodeURIComponent(escape(atob(b64)));
return JSON.parse(json);
} catch (e) {
console.error('Failed to decode payload', e);
return null;
}
}
function populateFromObject(obj, sourceLabel) {
if (!obj) return;
document.getElementById('record-id').innerText = sourceLabel || '';
document.getElementById('record-name').innerText = obj.name || sourceLabel || 'Bookmarklet';
document.getElementById('record-description').innerText = obj.description || '';
const code = obj.code || '';
document.getElementById('record-code').innerText = code;
document.getElementById('test-bookmarklet').href = code || '#';
}
(function init() {
const params = new URLSearchParams(window.location.search);
const payload = params.get('payload');
const id = params.get('id');
if (payload) {
const obj = decodePayload(payload);
if (obj) {
populateFromObject(obj, 'Shared Bookmarklet');
document.getElementById('record-id').innerText = 'shared';
return;
}
document.getElementById('record-name').innerText = 'Invalid payload';
return;
}
if (id) {
document.getElementById('record-id').innerText = id;
// support local saved items saved under 'hacklet:<id>' in localStorage
if (id.startsWith('local-')) {
try {
const raw = localStorage.getItem('hacklet:' + id);
if (raw) {
const obj = JSON.parse(raw);
populateFromObject(obj, 'Local Bookmarklet');
return;
}
document.getElementById('record-name').innerText = 'Local record not found';
return;
} catch (e) {
console.error('Error reading local record', e);
document.getElementById('record-name').innerText = 'Error reading local record';
return;
}
}
// fallback to remote fetch for non-local ids
const url = 'https://api2.hackclub.com/v0.1/Hacklet/Hacklet%20Project%20Submission';
fetch(url)
.then((response) => response.json())
.then((data) => {
const record = data.find((item) => item.id == id);
if (record) {
populateFromObject({
name: record.fields['Project Name'],
description: record.fields['Description'],
code: record.fields['Bookmarklet Code']
}, 'Remote Bookmarklet');
} else {
document.getElementById('record-name').innerText = 'Record not found';
}
})
.catch((error) => {
document.getElementById('record-name').innerText = 'Error fetching data';
console.error('Error:', error);
});
} else {
document.getElementById('record-name').innerText = 'No ID or payload provided in URL';
}
})();
function testBookmarklet() {
const code = document.getElementById('record-code').innerText;
if (!code.trim().startsWith('javascript:')) {
alert('Bookmarklet must start with "javascript:"');
return;
}
const cleanCode = code.replace('javascript:', '');
try {
new Function(cleanCode)();
} catch (error) {
console.error('Error executing bookmarklet, attempting URI decoded version...', error);
try {
const decoded = decodeURIComponent(cleanCode);
new Function(decoded)();
} catch (error) {
console.error('Error executing bookmarklet:', error);
alert('Error in bookmarklet code: ' + error.message);
}
}
}
</script>
</body>
</html>