Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spotify URI Plugin (BBD)
/**
 * @name SpotifyURI
 * 
 * @authorLink https://twitter.com/evildaimyoh
 * @website https://akiba.cloud/
 * @source https://gist.github.com/S4WA/2e26a0595724f0a351acd2b4e9c6639d
 * @updateUrl https://gist.github.com/S4WA/2e26a0595724f0a351acd2b4e9c6639d/raw/SpotifyURI.plugin.js
 */
module.exports = (_ => {
const config = {
"info": {
"name": "SpotifyURI",
"author": "Drip",
"version": "1.6.2",
"description": "Spotify Utility Stuff"
},
"changeLog": {
"improved": {
"Canary Changes": "Preparing Plugins for the changes that are already done on Discord Canary"
}
}
};
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
getDescription () {return `The Library Plugin needed for ${config.info.name} is missing. Open the Plugin Settings to download it. \n\n${config.info.description}`;}
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
if (!e && b && b.indexOf(`* @name BDFDB`) > -1) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
else BdApi.alert("Error", "Could not download BDFDB Library Plugin, try again later or download it manually from GitHub: https://github.com/mwittrien/BetterDiscordAddons/tree/master/Library/");
});
}
load () {
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
this.downloadLibrary();
}
});
}
if (!window.BDFDB_Global.pluginQueue.includes(config.info.name)) window.BDFDB_Global.pluginQueue.push(config.info.name);
}
start () {this.load();}
stop () {}
getSettingsPanel () {
let template = document.createElement("template");
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${config.info.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
return template.content.firstElementChild;
}
} : (([Plugin, BDFDB]) => {
var _this;
var settings = {};
return class SpotifyURI extends Plugin {
onLoad () {
_this = this;
this.defaults = {
settings: {
previewTracks: true,
replaceToUri: true
}
};
}
onStart () {
this.forceUpdateAll();
}
onStop () {
this.forceUpdateAll();
}
observer(changes) {
this.uriStuff();
this.replaceToURI();
}
getSettingsPanel (collapseStates = {}) {
return BDFDB.PluginUtils.createSettingsPanel(this, [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
mini: false,
grow: 0,
label: "Preview Tracks",
value: settings.previewTracks,
onChange: value => {
settings.previewTracks = value;
BDFDB.DataUtils.save(settings, this, "settings");
this.SettingsUpdated = true;
}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch",
mini: false,
grow: 0,
label: "Replace URL to URI",
value: settings.replaceToUri,
onChange: value => {
settings.replaceToUri = value;
BDFDB.DataUtils.save(settings, this, "settings");
this.SettingsUpdated = true;
}
})
]);
}
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.DiscordUtils.rerenderAll();
}
}
forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.DiscordUtils.rerenderAll();
}
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 (settings.previewTracks) {
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 (!settings.replaceToUri) 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]}`);
}
}
};
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
})();
@S4WA
Copy link
Author

S4WA commented Feb 15, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment