Skip to content

Instantly share code, notes, and snippets.

@drench
Last active May 12, 2020 04:40
Show Gist options
  • Save drench/f6ca4f8658f7e76d637de2aca01082d6 to your computer and use it in GitHub Desktop.
Save drench/f6ca4f8658f7e76d637de2aca01082d6 to your computer and use it in GitHub Desktop.
Discogs Search Extras userscript
// ==UserScript==
// @name Discogs Extras
// @description Adds search links to Discogs pages and more
// @include https://www.discogs.com/*
// ==/UserScript==
class SearchSite {
constructor(doc) {
this.doc = doc;
}
get searchSelect () {
if (this.selectElement) return this.selectElement;
var doc = this.doc;
var ss = doc.createElement('select');
ss.setAttribute('id', 'search_sites');
var placeholder = doc.createElement('option');
placeholder.value = '';
placeholder.innerText = 'Search for this on…';
ss.appendChild(placeholder);
SearchSite.sites.forEach(function (site) {
var opt = doc.createElement('option');
opt.innerText = site;
opt.value = site;
ss.appendChild(opt);
});
this.selectElement = ss;
return this.selectElement;
}
}
SearchSite.sites = [
'allmusic.com',
'amazon.com',
'bandcamp.com',
'deepdiscount.com',
'ebay.com',
'forcedexposure.com',
'pitchfork.com',
'popsike.com',
'rateyourmusic.com',
'stevehoffman.tv',
'en.wikipedia.org',
'youtube.com'
];
const makeSearchHead = function() {
var head = document.createElement('div');
head.className = 'head';
head.innerHTML = ' ';
return head;
};
document.querySelectorAll('meta[property="og:title"]').forEach(function (metaTag) {
var ogTitle = metaTag.getAttribute('content');
var m = ogTitle.match(/^(.+) - (.+)$/);
if (!m) return;
var artist = document.getElementById('profile_title').querySelectorAll('a')[0].innerText.replace(/\s+\(\d+\)$/, '');
var title = m[2];
var profile = document.querySelector('#page_content .body .profile');
if (!profile) return;
var profileEnd = profile.querySelector('.clear_left');
if (!profileEnd) return;
var searchSite = new SearchSite(document);
var query = `"${encodeURIComponent(artist)}"+"${encodeURIComponent(title)}"`;
searchSite.searchSelect.addEventListener('change', function (event) {
var domain = event.target.value;
if (domain.length > 0) {
window.open(`https://duckduckgo.com/?q=site:${domain}+${query}`);
}
});
profile.insertBefore(makeSearchHead(), profileEnd);
profile.insertBefore(searchSite.searchSelect, profileEnd);
});
var zeroPad = (n) => (n < 10) ? `0${n}` : `${n}`;
var hhmmss = function(seconds) {
var minutes = Math.floor(seconds / 60);
seconds = seconds - (minutes * 60);
var hours = Math.floor(minutes / 60);
if (hours < 1) return `${minutes}:${zeroPad(seconds)}`;
minutes = minutes - (hours * 60);
return `${hours}:${zeroPad(minutes)}:${zeroPad(seconds)}`;
};
document.querySelectorAll('table.playlist').forEach(function (playlistTable) {
var durations = Array.from(playlistTable.querySelectorAll('.tracklist_track_duration span'));
var seconds = durations
.map((s)=> s.innerText.split(/:/, 2).map((t)=> parseInt(t), 10))
.map((t)=> isNaN(t[0]) ? 0 : (t[0]*60 + t[1]))
.reduce((a,c)=> a+c);
if (isNaN(seconds) || seconds < 1) return;
var playlistColumns = playlistTable.querySelector('tr.track').querySelectorAll('td').length;
var footer = document.createElement('tfoot');
var tr = document.createElement('tr');
tr.className = 'tracklist_track track_heading';
// Pad a column on pages with track numbers
var padCount = playlistColumns;
while (padCount > 2) {
tr.appendChild(document.createElement('td'));
padCount -= 1;
}
var labelElement = document.createElement('td');
labelElement.className = 'tracklist_track_title';
labelElement.innerHTML = '<span>Total Time</span>';
tr.appendChild(labelElement);
var sumElement = document.createElement('td');
sumElement.innerHTML = `<span>${hhmmss(seconds)}</span>`;
sumElement.className = 'tracklist_track_duration';
tr.appendChild(sumElement);
footer.appendChild(tr);
playlistTable.appendChild(footer);
});
@drench
Copy link
Author

drench commented Aug 25, 2019

When browsing Discogs do you find yourself wondering if this on Bandcamp, if can I buy it on Deepdiscount, or what is the Steve Hoffman forum consensus on the best pressing? This is for you.

With the tampermonkey extension for Chrome installed, click the "Raw" button on this gist and it should install this script. Here it is in action:

Screen Shot 2019-08-25 at 10 25 40 AM

Screen Shot 2019-08-25 at 10 25 19 AM

Screen Shot 2019-08-25 at 10 26 12 AM

I decided to have all searches land on Duckduckgo result pages because, as much as I like and use !bang searches, these rely on the search capabilities of the individual sites, which are rarely as good as DDG itself.

@drench
Copy link
Author

drench commented Apr 13, 2020

New feature a total time row in track listings (when tracks have times listed)

Screen Shot 2020-04-12 at 10 10 38 PM

PLEASE NOTE I changed the name to "Discogs Extras" so you may need to uninstall "Discogs Search Extras" script before adding this updated version.

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