Skip to content

Commit e2bcb73

Browse files
committed
Start reworking Taiwan ISRC for new design
1 parent b9360df commit e2bcb73

File tree

1 file changed

+92
-77
lines changed

1 file changed

+92
-77
lines changed
Lines changed: 92 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// ==UserScript==
22
// @name Taiwan ISRC to MusicBrainz
3-
// @version 1.1.0
3+
// @version 2.0.0
44
// @namespace http://www.agj.cl/
55
// @description Adds a link to any isrc.ncl.edu.tw record entry page that opens the Add Release form in MusicBrainz, prefilling it with that record's information.
66
// @license Unlicense
7-
// @include http*://isrc.ncl.edu.tw/music_view.aspx*
7+
// @include http*://isrc.ncl.edu.tw/C100/*
88
// @grant none
99
// ==/UserScript==
1010

@@ -43,87 +43,101 @@
4343
Array.isArray(item) ? r.concat(flatten(item)) : r.concat([item]),
4444
[]
4545
);
46+
const onFullLoad = (cb) =>
47+
/complete/.test(document.readyState)
48+
? setTimeout(cb, 0)
49+
: window.addEventListener("load", cb, {
50+
once: true,
51+
});
52+
const waitFor = (milliseconds) =>
53+
new Promise((resolve) => {
54+
setTimeout(resolve, milliseconds);
55+
});
4656

47-
// Get values.
57+
onFullLoad(async () => {
58+
// Get values.
4859

49-
const values = Array.from(selAll(".musicView > table > tbody > tr")).reduce(
50-
(r, el) => {
51-
const label = selIn(el, "th").textContent.trim();
52-
const value = selIn(el, "td")
53-
.textContent.trim()
54-
.replace(/^:\s+(\S.*)$/, "$1");
55-
if (//.test(label)) r.artist = value;
56-
else if (//.test(label)) r.artist = value;
57-
else if (//.test(label)) r.title = value;
58-
else if (//.test(label)) r.label = value;
59-
else if (//.test(label)) r.cat = value;
60-
else if (/EAN/.test(label)) r.barcode = value;
61-
else if (//.test(label)) r.date = value.split("/");
62-
return r;
63-
},
64-
{}
65-
);
66-
values.tracks = Array.from(selAll(".AllMusic fieldset a"))
67-
.map(get("textContent"))
68-
.map(call("trim"))
69-
.map((t) => t.replace(/^.+ ] :(.*)$/, "$1"));
60+
await waitFor(1000);
7061

71-
// Add submit link.
62+
const values = Array.from(sel(".table").querySelectorAll("tr")).reduce(
63+
(r, el) => {
64+
const label = selIn(el, "th").textContent.trim();
65+
const value = selIn(el, "td").textContent.trim();
66+
if (//.test(label)) r.artist = value;
67+
else if (//.test(label)) r.artist = value;
68+
else if (//.test(label)) r.title = value;
69+
else if (//.test(label)) r.label = value;
70+
else if (//.test(label)) r.cat = value;
71+
else if (/EAN\/UPC/.test(label)) r.barcode = value;
72+
else if (//.test(label)) r.date = value.split(".");
73+
return r;
74+
},
75+
{}
76+
);
7277

73-
const checkValue = (value) => (value === undefined ? "" : value);
74-
const input = (name, value) =>
75-
dom("input", { name: name, value: checkValue(value), type: "text" });
78+
values.tracks = Array.from(selAll("#songsTable a"))
79+
.map(get("textContent"))
80+
.map(call("trim"))
81+
.map((t) => t.replace(/^\[\S+\] \d+[.](.*)$/, "$1"));
7682

77-
const link = dom("a", null, "Add to MusicBrainz");
78-
const form = dom(
79-
"form",
80-
{
81-
name: "musicbrainz-submit",
82-
action: "https://musicbrainz.org/release/add",
83-
method: "post",
84-
"accept-charset": "utf-8",
85-
style: "display: none",
86-
},
87-
input("name", values.title),
88-
input("artist_credit.names.0.name", values.artist),
89-
input("labels.0.name", values.label),
90-
input("labels.0.catalog_number", values.cat),
91-
input("events.0.date.year", values.date[0]),
92-
input("events.0.date.month", values.date[1]),
93-
input("events.0.country", "TW"),
94-
input("barcode", values.barcode),
95-
input("urls.0.url", window.location.href),
96-
input("urls.0.link_type", "82"),
97-
input("language", "cmn"),
98-
input("script", "Hant"),
99-
input("status", "official"),
100-
input("mediums.0.format", "cd"),
101-
input("edit_note", "From Taiwan ISRC: " + window.location.href)
102-
);
103-
const container = dom("div", { id: "musicbrainz-submit" }, link, form);
83+
console.log({ values });
10484

105-
const trackCount = counter();
106-
flatten(
107-
values.tracks.map((title) => {
108-
const i = trackCount();
109-
return [
110-
input(`mediums.0.track.${i}.name`, title),
111-
input(`mediums.0.track.${i}.number`, i + 1),
112-
];
113-
})
114-
).map((el) => form.appendChild(el));
85+
// Add submit link.
11586

116-
sel("#mainContent > div").prepend(container);
117-
link.addEventListener("click", (e) => {
118-
form.submit();
119-
e.preventDefault();
120-
});
87+
const checkValue = (value) => (value === undefined ? "" : value);
88+
const input = (name, value) =>
89+
dom("input", { name: name, value: checkValue(value), type: "text" });
90+
91+
const link = dom("a", null, "Add to MusicBrainz");
92+
const form = dom(
93+
"form",
94+
{
95+
name: "musicbrainz-submit",
96+
action: "https://musicbrainz.org/release/add",
97+
method: "post",
98+
"accept-charset": "utf-8",
99+
style: "display: none",
100+
},
101+
input("name", values.title),
102+
input("artist_credit.names.0.name", values.artist),
103+
input("labels.0.name", values.label),
104+
input("labels.0.catalog_number", values.cat),
105+
input("events.0.date.year", values.date[0]),
106+
input("events.0.date.month", values.date[1]),
107+
input("events.0.country", "TW"),
108+
input("barcode", values.barcode),
109+
input("urls.0.url", window.location.href),
110+
input("urls.0.link_type", "82"),
111+
input("language", "cmn"),
112+
input("script", "Hant"),
113+
input("status", "official"),
114+
input("mediums.0.format", "cd"),
115+
input("edit_note", "From Taiwan ISRC: " + window.location.href)
116+
);
117+
const container = dom("div", { id: "musicbrainz-submit" }, link, form);
118+
119+
const trackCount = counter();
120+
flatten(
121+
values.tracks.map((title) => {
122+
const i = trackCount();
123+
return [
124+
input(`mediums.0.track.${i}.name`, title),
125+
input(`mediums.0.track.${i}.number`, i + 1),
126+
];
127+
})
128+
).map((el) => form.appendChild(el));
121129

122-
sel("head").append(
123-
dom(
124-
"style",
125-
null,
126-
`
130+
sel("#mainContent > div").prepend(container);
131+
link.addEventListener("click", (e) => {
132+
form.submit();
133+
e.preventDefault();
134+
});
135+
136+
sel("head").append(
137+
dom(
138+
"style",
139+
null,
140+
`
127141
#musicbrainz-submit {
128142
display: inline-flex;
129143
vertical-align: bottom;
@@ -137,6 +151,7 @@
137151
font-weight: bold;
138152
}
139153
`
140-
)
141-
);
154+
)
155+
);
156+
});
142157
})();

0 commit comments

Comments
 (0)