-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathgenerate_share_link.html
More file actions
211 lines (169 loc) · 7.5 KB
/
generate_share_link.html
File metadata and controls
211 lines (169 loc) · 7.5 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
199
200
201
202
203
204
205
206
207
208
209
210
211
<!-- =========================================================================================
COPY/PASTE FOR DEVELOPERS (MINIMAL SHARE-LINK GENERATOR)
=========================================================================================
Goal:
Create a URL that opens https://michaeleskin.com/abctools/abctools.html with your ABC
content already loaded, plus optional settings like tablature format and auto-play.
You need ONE of these payload styles:
(A) LZW / LZ-String (standard):
?lzw=<LZString.compressToEncodedURIComponent(ABC)>&...
Requirements:
- Include lz-string.min.js
- Call LZString.compressToEncodedURIComponent(abcText)
(B) Deflate / Pako (often shorter):
?def=<base64url(deflate(utf8(ABC)))>&...
Requirements:
- Include pako.min.js
- Encode ABC as UTF-8 bytes (TextEncoder in browsers)
- pako.deflate(bytes, {level: 6})
- Base64 encode bytes
- Convert Base64 -> Base64URL: +->-, /->_, strip trailing '='
Core tool URL (always the same):
https://michaeleskin.com/abctools/abctools.html
Common parameters used in this demo (you can add/remove as desired):
- format=<tablatureOption> (e.g. noten, whistle, uke, mandolin, etc.)
- name=<shareName> (URLSearchParams will encode it)
- play=1 (optional: Opens the tune(s) in the Player)
- editor=1 (optional: Opens the tunes(s) in the Editor)
- ssp=10 (example of another tool option)
Practical URL-length limit:
This demo enforces 8100 chars (safe-ish). If you exceed it, return null and
consider:
- switching from LZW to Deflate,
- removing optional params,
- splitting content, or
- using a hosted file approach instead of a URL payload.
Minimal includes (pick what you need):
<script src="lz-string.min.js"></script> // for LZW links
<script src="pako.min.js"></script> // for Deflate links
Then copy the "MINIMAL IMPLEMENTATION" functions below:
- generateShareLink_Minimal(...)
- compressABC_LZW(...)
- compressABC_Deflate(...) + Base64URL helpers (if using Deflate)
========================================================================================= -->
<script>
/* =========================================================================================
MINIMAL IMPLEMENTATION (LZW + optional Deflate)
Drop this into your page/app, then call:
const url = generateShareLink_Minimal({
abcText: abcString,
shareName: "My Tune Book", // will be URL-encoded for you
tablatureOption: "noten", // e.g. "whistle", "uke", "mandolin", etc.
addAutoPlay: true, // adds &play=1 to open the tune in the Player
// or:
// addOpenInEditor: true, // adds &editor=1 to open the tune in the Editor
compressionMode: "deflate" // "lzw" or "deflate"
});
========================================================================================= */
// Base URL of the ABC Transcription Tools
const ABC_TOOLS_BASE_URL = "https://michaeleskin.com/abctools/abctools.html";
// Practical max URL length used by this ecosystem
const SHARE_LINK_MAX_LEN = 8100;
/**
* Generate a share link that opens ABC Transcription Tools with ABC preloaded.
*
* @param {Object} opts
* @param {string} opts.abcText - Full ABC text payload to embed in the URL
* @param {string} opts.shareName - Display name/title shown by the tools
* @param {string} opts.tablatureOption - Value for the "format=" query param
* @param {boolean} opts.addAutoPlay - If true, appends "&play=1"
* @param {boolean} opts.addOpenInEditor - If true, appends "&editor=1"
* @param {"lzw"|"deflate"} opts.compressionMode - Which payload scheme to use
*
* @returns {string|null} Share link URL, or null if it would be too long
*/
function generateShareLink_Minimal(opts) {
opts = opts || {};
const abcText = (opts && opts.abcText) ? opts.abcText : "";
const tablatureOption = (opts && opts.tablatureOption) ? opts.tablatureOption : "noten";
const compressionMode = (opts && opts.compressionMode) ? opts.compressionMode : "deflate";
const shareName = (opts && opts.shareName) ? opts.shareName : "Share_Link";
let addAutoPlay = !!(opts && opts.addAutoPlay);
let addOpenInEditor = !!(opts && opts.addOpenInEditor);
// Only one should be enabled. If both are true, prefer Editor.
if (addAutoPlay && addOpenInEditor) {
addAutoPlay = false;
}
// 1) Create the compressed ABC payload for either lzw= or def=
let payloadParamName = "";
let payloadParamValue = "";
if (compressionMode === "deflate") {
payloadParamName = "def";
payloadParamValue = compressABC_Deflate(abcText); // Base64URL string
} else {
payloadParamName = "lzw";
payloadParamValue = compressABC_LZW(abcText); // EncodedURIComponent-safe string
}
// 2) Build the URL
// NOTE: URLSearchParams is used here for correctness (escaping, ordering, etc.)
const params = new URLSearchParams();
// The ABC payload itself (one of these is required)
params.set(payloadParamName, payloadParamValue);
// Optional tool configuration parameters
params.set("format", tablatureOption);
params.set("ssp", "10"); // example of passing another option
params.set("name", shareName);
if (addAutoPlay) {
params.set("play", "1");
}
if (addOpenInEditor) {
params.set("editor", "1");
}
const url = `${ABC_TOOLS_BASE_URL}?${params.toString()}`;
// 3) Enforce practical max length
return (url.length > SHARE_LINK_MAX_LEN) ? null : url;
}
/* ---------------------------
LZW (standard) compression
---------------------------
Uses LZ-String's URL-safe output. This is the simplest method.
REQUIREMENT:
Include lz-string.min.js so LZString is defined.
*/
function compressABC_LZW(abcText) {
if (typeof LZString === "undefined") {
throw new Error("LZString (lz-string.min.js) is not loaded.");
}
return LZString.compressToEncodedURIComponent(abcText);
}
/* ---------------------------
Deflate (often shorter) compression
---------------------------
Steps:
1) UTF-8 encode the string -> bytes
2) pako.deflate(bytes, {level:6}) -> compressed bytes
3) Base64 encode bytes
4) Convert Base64 to Base64URL for query-string safety
REQUIREMENTS:
- Include pako.min.js so pako is defined
- Modern browser: TextEncoder exists (most do)
*/
function compressABC_Deflate(abcText) {
if (typeof pako === "undefined") {
throw new Error("pako (pako.min.js) is not loaded.");
}
if (typeof TextEncoder === "undefined") {
throw new Error("TextEncoder is not available in this environment.");
}
const utf8Bytes = new TextEncoder().encode(abcText);
const deflatedBytes = pako.deflate(utf8Bytes, { level: 6 });
return bytesToBase64URL(deflatedBytes);
}
// --- Base64URL helpers (Deflate mode) ---
function bytesToBase64(bytes) {
// Convert raw bytes to a binary string for btoa().
// (This is fine for moderate sizes; for extremely large payloads you might chunk it.)
let binary = "";
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function bytesToBase64URL(bytes) {
// Base64URL: replace +/, strip =
return bytesToBase64(bytes)
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
</script>