Deezer - Fix favorite tracks sorting

try to take over the world!

当前为 2019-01-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Deezer - Fix favorite tracks sorting
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://www.deezer.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. function rafAsync() {
  14. return new Promise(resolve => {
  15. requestAnimationFrame(resolve); //faster than set time out
  16. });
  17. }
  18.  
  19. function checkElement(selector) {
  20. if (document.querySelector(selector) === null) {
  21. return rafAsync().then(() => checkElement(selector));
  22. } else {
  23. return Promise.resolve(true);
  24. }
  25. }
  26.  
  27. let sortTracks = () => {
  28. checkElement('.dropdown .dropdown-toggle')
  29. .then((element) => {
  30. document.querySelectorAll('.dropdown .dropdown-toggle')[0].click();
  31. document.querySelectorAll('.dropdown .dropdown-menu .dropdown-item a')[0].click();
  32. });
  33. };
  34.  
  35. (function(history){
  36. var pushState = history.pushState;
  37. history.pushState = function(state) {
  38. if (typeof history.onpushstate == "function") {
  39. history.onpushstate({state: state});
  40. }
  41. if (arguments[2].match(/loved$/)) {
  42. sortTracks();
  43. }
  44. return pushState.apply(history, arguments);
  45. };
  46. })(window.history);
  47.  
  48. if (window.location.href.match(/loved$/)) {
  49. sortTracks();
  50. }
  51. })();