Greasy Fork is available in English.

RedditP Collect

Collect cool stuff while staying in the flow. Download them at the end.

  1. // ==UserScript==
  2. // @name RedditP Collect
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Collect cool stuff while staying in the flow. Download them at the end.
  6. // @author Leeroy
  7. // @match http://redditp.com/*
  8. // @grant GM_addStyle
  9. // @grant GM_download
  10. // @noframes
  11. // @nocompat Chrome
  12. // ==/UserScript==
  13. /* global rp */
  14.  
  15. /*
  16. WARNING GM_download must be set to Browser API in Tampermonkey settings
  17. */
  18. rp.savedURLs = rp.savedURLs || [];
  19. if(saveEvent) document.removeEventListener("keyup", saveEvent);
  20.  
  21. GM_addStyle('#timeToNextSlide{width:2.4rem}.sort-link{display:table}#subredditUrl.with-sort{padding-top:0;padding-bottom:0;max-width:145px;white-space:nowrap;}.numberButton.saved,.numberButton.saved.active{background:#00C853;color:#eee!important}.numberButton.saved.downloaded{background:#006429}@keyframes burst{0%{opacity:.6}50%{-webkit-transform:scale(1.8);-ms-transform:scale(1.8);transform:scale(1.8);opacity:0}100%{opacity:0}}body.saved #titleDiv:before{content:"\\2705 ";color:#00C853;position:fixed;width:99%;text-align:right;right:1em;top:1em;font-weight:700;font-size:large}body.confirm:after{-webkit-animation:burst 2s forwards linear;animation:burst 2s forwards linear;content:"\\2705";color:#00C853;background-color:#fff;border-radius:50%;position:fixed;font-size:2rem;line-height:4rem;text-align:center;z-index:999;height:2em;width:2em;margin:auto;top:0;left:0;bottom:0;right:0}');
  22.  
  23. function getFilename(path) {
  24. return path.split('\\').pop().split('/').pop();
  25. }
  26.  
  27. var downloadSaved = function() {
  28. // Download collection one by one (must be allowed by user)
  29. rp.savedURLs.forEach(function(url) {
  30. GM_download(url, getFilename(url));
  31. });
  32. [].map.call(document.querySelectorAll('.numberButton.saved'), function(el) {
  33. el.classList.add('downloaded');
  34. });
  35. rp.savedURLs = [];
  36. };
  37.  
  38. rp.saveActive = function() {
  39. var activeNumberButton = document.querySelector(".numberButton.active");
  40.  
  41. var url = rp.photos[rp.session.activeIndex].type === "gfycat" ? document.querySelector("video source").src : rp.photos[rp.session.activeIndex].url;
  42. url = url.replace(".gifv",".webm");
  43.  
  44. var index = rp.savedURLs.indexOf(url);
  45. if (index !== -1) {
  46. activeNumberButton.classList.remove("saved");
  47. rp.savedURLs.splice(index, 1);
  48. checkSaved();
  49. } else {
  50. activeNumberButton.classList.add("saved");
  51. rp.savedURLs.push(url);
  52. checkSaved();
  53. }
  54. };
  55.  
  56. var saveEvent = function(e) {
  57. var S_KEY = 83,
  58. D_KEY = 68;
  59. if(e.keyCode === S_KEY) {
  60. rp.saveActive();
  61. document.body.classList.toggle("confirm", (document.querySelector(".active.saved")));
  62. }
  63. if(e.keyCode === D_KEY) {
  64. downloadSaved();
  65. }
  66. };
  67.  
  68. document.addEventListener("keyup", saveEvent, false);
  69.  
  70. var checkSaved = function() {
  71. document.body.classList.toggle("saved", document.querySelector(".active.saved"));
  72. document.body.classList.remove("confirm");
  73. };
  74.  
  75. var enableLoop = function() {
  76. // Always enable loop on videos
  77. if(rp.photos[rp.session.activeIndex].type !== "image") document.querySelector("video").loop = true;
  78. };
  79.  
  80. var observer = (function() {
  81. // select the target node
  82. var target = document.querySelector('#pictureSlider');
  83.  
  84. // create an observer instance
  85. var observer = new MutationObserver(function(mutations) {
  86. checkSaved();
  87. // On the first mutation the video element isn't ready yet
  88. if(mutations[0].removedNodes.length) enableLoop();
  89. });
  90. // pass in the target node, as well as the observer options
  91. observer.observe(target, {childList: true});
  92. return observer;
  93. })();