Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Easy to open your Spotify URI. (Better Discord Plugin)
/**
* @name SpotifyURI
*
* @authorLink https://twitter.com/evildaimyoh
* @website https://akiba.cloud/
* @source https://gist.github.com/S4WA/2e26a0595724f0a351acd2b4e9c6639d
*/
class SpotifyURI {
getName() {
return "Spotify URI";
}
getDescription() {
return "Spotify Utility Stuff";
}
getVersion() {
return "1.6.0";
}
getAuthor() {
return "Drip";
}
getRaw() {
return "https://gist.github.com/S4WA/2e26a0595724f0a351acd2b4e9c6639d/raw/SpotifyURI.plugin.js";
}
start() {
let script = document.createElement("script");
script.setAttribute("id", "BDFDBLibraryScript");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
script.addEventListener("load", () => { this.init(); });
if (!document.querySelector("head script#BDFDBLibraryScript")) {
document.head.appendChild(script);
} else if (this.isBdfdbLoaded()) {
this.init();
}
}
load() {
if (this.isBdfdbLoaded()) BDFDB.PluginUtils.load(this);
// BDFDB.DiscordUtils.shake();
}
init() {
// register the plugin for update
if (!window.PluginUpdates || typeof window.PluginUpdates !== "object") {
window.PluginUpdates = {"plugins": {} };
}
window.PluginUpdates.plugins[this.getRaw()] = {
"name": "SpotifyURI",
"raw": this.getRaw(),
"version": this.getVersion()
};
BDFDB.PluginUtils.checkUpdate("SpotifyURI", this.getRaw());
// load settings
options = BDFDB.DataUtils.load("SpotifyURI", "options");
}
constructor () {
this.changelog = {
"added": [
[
"This",
"We added the changelog ¯\\_(ツ)_/¯"
]
],
"improved": [
[
"New Message",
"When sending a new URI message, it now takes less time to replace the A tag with a embed"
]
],
"fixed": [
[
"Increased Embed",
"Fixed a bug that increased embed when editing a URI message"
]
]
};
}
stop() { }
getSettingsPanel() {
if (!this.isBdfdbLoaded()) return;
return BDFDB.PluginUtils.createSettingsPanel(this, [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
mini: false,
grow: 0,
label: "Preview Tracks",
value: options["preview-tracks"],
onChange: value => {
options["preview-tracks"] = value;
BDFDB.DataUtils.save(options["preview-tracks"], "SpotifyURI", "options", "preview-tracks");
}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
mini: false,
grow: 0,
label: "Replace URL to URI",
value: options["replace-to-uri"],
onChange: value => {
options["replace-to-uri"] = value;
BDFDB.DataUtils.save(options["replace-to-uri"], "SpotifyURI", "options", "replace-to-uri");
}
})
]);
}
observer(changes) {
this.uriStuff();
this.replaceToURI();
}
isBdfdbLoaded() {
return (window.BDFDB && typeof BDFDB == "object" && BDFDB.loaded);
}
uriStuff() {
let target = $(".da-contents .da-messageContent"), max = target.length;
for (var i = 0; i < max; i++) {
let text = $(target[i]).text();
if (!text) {
continue;
}
let html = target[i].innerHTML;
if (html.includes(`class="spotify-uri"`)) {
continue;
}
if (!text.includes("spotify:track:") && !text.includes("spotify:playlist:") && !text.includes("spotify:album:") && !text.includes("spotify:artist:") && !text.includes("spotify:user:")) {
continue;
}
let match = html.match(/spotify:(track:|playlist:|album:|artist:|user:)([a-zA-Z0-9?%&][^\s]+)/);
target[i].innerHTML = html.replace(match[0], `<a href="${match[0]}" target="_blank" class="spotify-uri" title="${match[0]}">${match[0]}</a>`);
// target[i].innerHTML = `<a href="${html}" target="_blank" class="spotify-uri">${html}</a> `;
// put the embed
let sp = html.split(":");
if (sp[1] == "user") {
continue;
}
if (options["preview-tracks"]) {
let embedURL = `https://open.spotify.com/embed/${sp[1]}/${sp[2]}`, height = (sp[1] == "track") ? 80 : 300;
// when edit message that contains spotify uri embed, remove it once and add again.
// it is for not increase it.
$(target[i]).parent().find(".spotify-uri-embed").remove();
$(target[i]).parent().append(
$(`<div class="da-container">
<iframe class="spotify-uri-embed" frameborder="0" style="width: 400px; height: ${height}px;" src="${embedURL}">
</div>`)
);
}
}
}
// any spotify links will be replaced to uri
replaceToURI() {
if (!options["replace-to-uri"]) return;
// ".da-contents .da-messageContent a"
let target = $("a"), length = target.length;
for (var i = 0; i < length; i++) {
let text = $(target[i]).attr("href");
if (!text || !text.includes("open.spotify.com")) {
continue;
}
let sp = text.replace("https://", "").replace("http://", "").split("/");
$(target[i]).attr("href", `spotify:${sp[1]}:${sp[2]}`);
}
}
}
var options = {
"preview-tracks": true,
"replace-to-uri": true
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.