SoundCloudLog

Log played SoundCloud tracks in browsing history to avoid losing music. More detailed instructions and logMode setting inside code.

  1. // ==UserScript==
  2. // @name SoundCloudLog
  3. // @description Log played SoundCloud tracks in browsing history to avoid losing music. More detailed instructions and logMode setting inside code.
  4. // @namespace gercomsci
  5. // @version 1.1
  6. // @author Gercomsci
  7. // @run-at document-end
  8. // @grant none
  9. //
  10. // @match *://soundcloud.com/*
  11. // ==/UserScript==
  12.  
  13. /* SoundCloudLog – Never lose that tune again!
  14. This is my first ever user script. Feel free to fork and improve.
  15. Because SoundCloud is a progressive web application, music playing in background is not logged in the browsing history; only when the playback page is actually opened.
  16. This user script makes clever use of browsers' logging of pushState and anchor URLs in the history, and could have saved several tracks I've lost.
  17. */
  18.  
  19. // == Instructions ==
  20. /* Originally, I was going to release separate "variant A" and "variant B" scripts for two distinct logging modes. But I decided to combine both variants into a single script for simplicity. This preference can easily be adjusted in the source code of the script, in the "preferences" section.
  21. Log mode A: "pushState" generates a shareable URL. Log mode B: "URL anchor" changes the URL back to prevent interfering with history navigation. For maximum compatibility, "B" is preferred, thus it is preset.
  22. I might implement an in-website user interface for settings in future, but for now, this fullfills its purpose.
  23. */
  24.  
  25. // ----
  26. // == Preferences ==
  27. var logMode = "B"; // A: pushState B: URL anchor
  28.  
  29. // ----
  30. // == Main code ==
  31.  
  32. // Function to locate relative track URL
  33. var trackURL;
  34. function updateTrackURL() {
  35. trackURL = document.getElementsByClassName("playbackSoundBadge__titleLink")[0].getAttribute("href");
  36. logTrackURL();
  37. }
  38.  
  39. function updateDate() {
  40. timestamp = new Date().toISOString();
  41. // Very practical! https://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript/8563517#8563517
  42. }
  43.  
  44. function hasTrackChanged() {
  45. updateDate();
  46. if ( trackURL !== document.getElementsByClassName("playbackSoundBadge__titleLink")[0].getAttribute("href")) {
  47. updateTrackURL(); // read new track URL
  48. logTrackURL(); // record it to browsing history
  49. console.log('SoundCloudLog [mode '+logMode+'] '+timestamp+': Track URL changed to '+trackURL); // for debugging
  50. }
  51. else { (0); } // Placeholder, in case necessary later.
  52. }
  53.  
  54. function logTrackURL() {
  55. if ( logMode == "A" ) { // using pushState
  56. window.history.pushState('page2', 'Title', trackURL+'#SoundCloudLog-'+timestamp);
  57. } else {
  58. if ( logMode == "B" ) { // using anchor URL
  59. window.location.href = ("#SoundCloudLog-"+timestamp+"-"+trackURL);
  60. if (navigator.userAgent.indexOf("Chrome") == -1) { // do not do this on Chromium-based browsers, as it navigates back from the non-anchored state
  61. javascript:history.go(-1); // Remove anchor after logging, to prevent navigation interference.
  62. };
  63. window.history.pushState('page2','Title',window.location.toString().split("#")[0]); // remove anchor to make URL of playing track more suitable for sharing. (for mode B, this is only fallback code.)
  64. } else { // If logMode specified in settings is invalid:
  65. console.log('SoundCloudLog Error '+timestamp+': Invalid logMode: '+logMode); //debug
  66. alert('SoundCloudLog Error: Invalid logMode: '+logMode+'.\nCheck the "Instructions" section of this user script\'s source code for help.'); //notify user
  67. logMode = "B"; // reset mode to B, for foolproofness
  68. }
  69. }
  70. }
  71.  
  72. // Check for change every second. If change detected, apply to URL.
  73. changeCheck = setInterval(function() { hasTrackChanged() }, 1000);
  74.  
  75. // Original script author's committed identity: 66ace7785a9f