-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuno-notepad.user.js
196 lines (161 loc) · 4.32 KB
/
suno-notepad.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
// ==UserScript==
// @name Suno Notepad
// @namespace https://github.com/LeoDupont/userscripts
// @version 1.2
// @description Lets you annotate Suno tracks. Uses localStorage.
// @author LeoDupont
// @match https://app.suno.ai/*
// @icon https://app.suno.ai/logo2.svg
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==
////////////////////////////////////////////////////////////////////
// Suno Notepad
//
// Displays a simple text input on each track
// * on the tracks list
// * on each track's page
//
// Uses the browser's localStorage to store notes.
// => notes are not synced anywhere on the Internet.
////////////////////////////////////////////////////////////////////
var $ = window.$; // Declaration
setTimeout(init, 500);
async function init() {
'use strict';
console.log("==== Starting Suno Notepad ====")
mountNotepads();
onUrlChange(mountNotepads);
showManualRefreshButton();
addGlobalStyles(`
.suno-notepad {
width: 30rem;
margin: 0 1rem;
background-color: #242a34;
color: white;
}
.css-pjl8ph .suno-notepad {
display: block;
width: 100%;
margin: 0.1rem 0;
}
.css-lw6smx .suno-notepad {
display: block;
margin: 0.1rem 0;
}
`);
}
// =======================================================
// == Helpers
// =======================================================
async function mountNotepads() {
// === Track Page ===
var trackId = getTrackIdFromUrl(document.URL);
if (trackId) {
loadNotepad(trackId, (notepad) => {
$('.css-lw6smx .css-1f0wxn3 p').prepend(notepad);
});
}
// === Create & Library Pages ===
var tracksElms = await waitForElms('.css-ef2rl9');
$(tracksElms).each(function (i, elm) {
var trackId = elm.attributes['data-clip-id'].value;
if (trackId) {
loadNotepad(trackId, (notepad) => {
$(elm).find('.css-89mcmc').append(notepad);
});
}
});
}
function getTrackIdFromUrl(url) {
var match = url.match(/\/([\w-]{36})/);
if (match) {
return match[1];
}
}
function onUrlChange(callback) {
var currentLocation = location.href;
new MutationObserver(mutations => {
if (currentLocation !== location.href) {
currentLocation = location.href;
callback();
}
}).observe(document.body, {
childList: true,
subtree: false
});
}
function waitForElms(selector) {
return new Promise(resolve => {
if (document.querySelectorAll(selector).length > 0) {
return resolve(document.querySelectorAll(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelectorAll(selector).length > 0) {
// observer.disconnect();
resolve(document.querySelectorAll(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
// =======================================================
// == UI
// =======================================================
function loadNotepad(trackId, callback) {
const inputId = 'suno-notepad-' + trackId;
if ($("#" + inputId).length != 0) {
return;
}
const trackNotes = loadNotes(trackId);
const notepad = $('<input>')
.attr({
id: inputId,
class: 'suno-notepad',
name: 'suno-notepad',
value: trackNotes,
placeholder: 'Notepad...',
autocomplete: 'off',
})
.keyup(function (e) {
var newNotes = e.target.value;
console.log('[SUNO NOTEPAD] Storing new notes for ' + trackId + ': ' + newNotes);
saveNotes(trackId, newNotes);
});
callback(notepad);
}
function showManualRefreshButton() {
$('<button>')
.attr({
id: 'suno-notepad-manual-refresh',
style: 'position: fixed; bottom: 1rem; right: 1rem; z-index: 9999',
title: 'Notepad Refresh',
type: 'button',
})
.text('Notepad Refresh')
.appendTo(document.body)
.click(function (e) {
mountNotepads();
});
}
function addGlobalStyles(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
// =======================================================
// == Storage
// =======================================================
function loadNotes(elmId) {
return localStorage.getItem('notepad-' + elmId);
}
function saveNotes(elmId, notes) {
localStorage.setItem('notepad-' + elmId, notes);
}