Freesound.org - license filter

Keeps only cc0 and (optionally) cc-by and sampling+ found items in search results page.

  1. // ==UserScript==
  2. // @name Freesound.org - license filter
  3. // @namespace http://monnef.tk/
  4. // @version 0.1
  5. // @description Keeps only cc0 and (optionally) cc-by and sampling+ found items in search results page.
  6. // @match http://www.freesound.org/search/*
  7. // @copyright 2014+, monnef
  8. // @require https://code.jquery.com/jquery-2.1.1.min.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. var ACTION_ENUM = {
  13. REMOVE: "remove", // completely removes element
  14. FOLD: "fold", // folds element, can be unfolded by a click
  15. TRANSPARENT: "transparent" // makes element partially transparent, on mouse over is fully visible
  16. };
  17.  
  18. // settigns
  19. var action = ACTION_ENUM.FOLD; // see ACTION_ENUM
  20. var showCCBY = true; // CC-Attribution
  21. var showSampling = false; // CC-Sampling+
  22. // end of settings
  23.  
  24. var debug = false;
  25.  
  26. this.$ = this.jQuery = jQuery.noConflict(true);
  27.  
  28. function addStyle(style) {
  29. style = style instanceof Array ? style.join('\n') : style;
  30. $("head").append($('<style type="text/css">' + style + '</style>'));
  31. }
  32.  
  33. var prClass = "hideable1357210";
  34. var prAnimLen = "0.5";
  35.  
  36. addStyle([
  37. '.' + prClass + ' {',
  38. ' -webkit-transition: opacity ' + prAnimLen + 's;',
  39. ' -moz-transition: opacity ' + prAnimLen + 's;',
  40. ' -ms-transition: opacity ' + prAnimLen + 's;',
  41. ' transition: opacity ' + prAnimLen + 's;',
  42. ' opacity: 0.05;',
  43. '}',
  44. '.' + prClass + ':hover {',
  45. ' opacity: 1;',
  46. '}'
  47. ]);
  48.  
  49. function doHide(elem) {
  50. var e = $(elem);
  51. console.log("Hiding " + e.attr("id") + ".");
  52. switch (action) {
  53. case ACTION_ENUM.REMOVE:
  54. e.remove();
  55. break;
  56.  
  57. case ACTION_ENUM.FOLD:
  58. var title = $("a.title", e).html();
  59. var id = e.attr("id");
  60. var text = $("<span />").html("[+] "+title);
  61. text.css("cursor","pointer");
  62. text.click(function() {
  63. $("#" + id).toggle("fast");
  64. });
  65. var divLink = $("<div />").html(text).css("margin-bottom", "8px");
  66. e.before(divLink);
  67. e.hide();
  68. e.wrap("<div style=\"border-left: 2px solid #ccc; margin-left:8px; padding-left:4px;\"></div>");
  69. break;
  70.  
  71. case ACTION_ENUM.TRANSPARENT:
  72. e.addClass(prClass);
  73. break;
  74.  
  75. default:
  76. console.log("Unknown action: " + action);
  77. }
  78. }
  79.  
  80. var licRegex = /\/([^/]*?)\.png/;
  81.  
  82. $(document).ready(function() {
  83. var items = $(".sample_player_small");
  84. console.log("Found " + items.size() + " items.");
  85. items.each(function(index, elem) {
  86. // console.log("Processing " + $(this).attr("id") + " ("+index+").");
  87. if (debug) $(this).css("border", "1px solid red");
  88. var img = $("img.cc_license", this);
  89. var imgSrc = img.attr("src");
  90. if (licRegex.test(imgSrc)) {
  91. var lic = licRegex.exec(imgSrc)[1];
  92. var hide;
  93. if (lic === "nolaw") {
  94. // cc0, fine
  95. hide = false;
  96. } else if (lic === "by") {
  97. hide = !showCCBY;
  98. } else if (lic === "sampling") {
  99. hide = !showSampling;
  100. } else if (lic === "bync") {
  101. hide = true;
  102. } else {
  103. console.log("Unknown license: " + lic);
  104. hide = false;
  105. }
  106.  
  107. if (hide) {
  108. doHide(this);
  109. }
  110. } else {
  111. console.log("Can't get source of image.");
  112. }
  113. });
  114. $(".search_paginator").last().before($("<div>Script for filtering by license was created by <a href='http://monnef.tk'>monnef</a>. Please consider supporting me via <a href='https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=U6PGB7P24WWSU&lc=EC&item_name=freesound%2eorg%20filtering%20script&currency_code=CZK&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted'>PayPal</a> or <a href='http://adf.ly/2536344/freesoundorg-script'>AdFly</a>.</div>"));
  115. });