Freesound.org - license filter

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name       Freesound.org - license filter
// @namespace  http://monnef.tk/
// @version    0.1
// @description   Keeps only cc0 and (optionally) cc-by and sampling+ found items in search results page.
// @match      http://www.freesound.org/search/*
// @copyright  2014+, monnef
// @require     https://code.jquery.com/jquery-2.1.1.min.js
// @grant       none
// ==/UserScript==

var ACTION_ENUM = {
    REMOVE: "remove", // completely removes element
    FOLD: "fold", // folds element, can be unfolded by a click
    TRANSPARENT: "transparent" // makes element partially transparent, on mouse over is fully visible
};

// settigns
var action = ACTION_ENUM.FOLD; // see ACTION_ENUM
var showCCBY = true; // CC-Attribution
var showSampling = false; // CC-Sampling+
// end of settings

var debug = false;

this.$ = this.jQuery = jQuery.noConflict(true);

function addStyle(style) {
    style = style instanceof Array ? style.join('\n') : style;
    $("head").append($('<style type="text/css">' + style + '</style>'));
}

var prClass = "hideable1357210";
var prAnimLen = "0.5";

addStyle([
    '.' + prClass + ' {',
    '    -webkit-transition: opacity ' + prAnimLen + 's;',
    '    -moz-transition: opacity ' + prAnimLen + 's;',
    '    -ms-transition: opacity ' + prAnimLen + 's;',
    '    transition: opacity ' + prAnimLen + 's;',
    '    opacity: 0.05;',
    '}',
    '.' + prClass + ':hover {',
    '    opacity: 1;',
    '}'
]);

function doHide(elem) {
    var e = $(elem);
    console.log("Hiding " + e.attr("id") + ".");
    switch (action) {
        case ACTION_ENUM.REMOVE:
            e.remove();
            break;

        case ACTION_ENUM.FOLD:
            var title = $("a.title", e).html();
            var id = e.attr("id");
            var text = $("<span />").html("[+] "+title);
            text.css("cursor","pointer");
            text.click(function() {
                $("#" + id).toggle("fast");
            });
            var divLink = $("<div />").html(text).css("margin-bottom", "8px");
            e.before(divLink);
            e.hide();
            e.wrap("<div style=\"border-left: 2px solid #ccc; margin-left:8px; padding-left:4px;\"></div>");
            break;

        case ACTION_ENUM.TRANSPARENT:
            e.addClass(prClass);
            break;

        default:
            console.log("Unknown action: " + action);
    }
}

var licRegex = /\/([^/]*?)\.png/;

$(document).ready(function() {
    var items = $(".sample_player_small");
    console.log("Found " + items.size() + " items.");
    items.each(function(index, elem) {
        // console.log("Processing " + $(this).attr("id") + " ("+index+").");
        if (debug) $(this).css("border", "1px solid red");
        var img = $("img.cc_license", this);
        var imgSrc = img.attr("src");
        if (licRegex.test(imgSrc)) {
            var lic = licRegex.exec(imgSrc)[1];
            var hide;
            if (lic === "nolaw") {
                // cc0, fine
                hide = false;
            } else if (lic === "by") {
                hide = !showCCBY;
            } else if (lic === "sampling") {
                hide = !showSampling;
            } else if (lic === "bync") {
                hide = true;
            } else {
                console.log("Unknown license: " + lic);
                hide = false;
            }

            if (hide) {
                doHide(this);
            }
        } else {
            console.log("Can't get source of image.");
        }
    });
    $(".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>"));
});