Bandcamp Current Title

Shows currently playing track in the page/window/tab title

  1. // ==UserScript==
  2. // @name Bandcamp Current Title
  3. // @version 1.3
  4. // @author raina
  5. // @namespace raina
  6. // @description Shows currently playing track in the page/window/tab title
  7. // @license GPLv3
  8. // @include /^https?:\/\//
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. window.self === window.top && window.siteroot && "https://bandcamp.com" == window.siteroot && (function() {
  13.  
  14. const title = document.head.querySelector(`title`);
  15. const album = document.body.querySelector(`#name-section h2.trackTitle`).textContent.trim();
  16. const albumArtist = document.body.querySelector(`#name-section h3 span a`).textContent.trim();
  17. const currentTrack = document.body.querySelector('.inline_player .title');
  18. const year = document.body.querySelector(`.tralbum-credits`).textContent.match(/(?<=released.*)(\d{4})(?=\n)/)[0];
  19. let artist, track;
  20.  
  21. let llamaTime;
  22. const llamaScroll = () => {
  23. observer.disconnect();
  24. title.textContent = title.textContent.slice(0,3) + title.textContent.slice(4) + title.textContent.slice(3,4);
  25. observer.observe(title, {childList:true});
  26. }
  27.  
  28. const observer = new MutationObserver((mutationList, observer) => {
  29.  
  30. let titleString = currentTrack.textContent;
  31.  
  32. observer.disconnect();
  33.  
  34. if (mutationList[0].addedNodes[0].data.includes("▶︎")) {
  35. if (track = titleString.split(" - ")[1]) {
  36. artist = titleString[0];
  37. } else {
  38. artist = albumArtist;
  39. track = currentTrack.textContent.trim();
  40. }
  41. title.textContent = `▶︎ ${album}, ${year}) ${artist} ${track} (`.replace(/ /g, "\xa0");
  42. llamaTime = setInterval(llamaScroll, 250);
  43. } else {
  44. clearInterval(llamaTime);
  45. title.textContent = title.dataset.originalTitle;
  46. }
  47.  
  48. observer.observe(title, {childList:true});
  49.  
  50. });
  51.  
  52. title.dataset.originalTitle = title.textContent.trim();
  53. observer.observe(title, {childList:true});
  54.  
  55. }());