Skip to content

Instantly share code, notes, and snippets.

@ide-an
Last active November 16, 2017 19:54
Show Gist options
  • Select an option

  • Save ide-an/5509137 to your computer and use it in GitHub Desktop.

Select an option

Save ide-an/5509137 to your computer and use it in GitHub Desktop.

Revisions

  1. ide-an revised this gist Nov 29, 2014. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions bgmsharehouse-playhistory.user.js
    Original file line number Diff line number Diff line change
    @@ -56,23 +56,23 @@
    };
    var current_strategy = null;
    var strategies = [
    new Strategy("一番最近っぽい30件",
    new Strategy("一番最近っぽい50件",
    function(history){
    var n = 30;
    var n = 50;
    if(history.length<n){
    return history.slice().reverse();
    }else{
    return history.slice(history.length - n).reverse();
    }
    }),
    new Strategy("一番昔っぽい30件",
    new Strategy("一番昔っぽい50件",
    function(history){
    var n = 30;
    var n = 50;
    return history.slice(0, n);
    }),
    new Strategy("ランダムっぽい30件",
    new Strategy("ランダムっぽい50件",
    function(history){
    var n = 30;
    var n = 50;
    var h = history.slice();
    var res = [];
    for(var i=0;i<n;i++){
    @@ -129,6 +129,8 @@
    '<div id="play-history-pane" ',
    'style="overflow: scroll;width: 100%;height: 10em;background-color: #fff;"',
    '>',
    '<div id="play-history-inner-pane" style="width: 10000%;">',
    '</div>',
    '</div>'
    ].join("");
    var select = div.getElementsByTagName("select")[0];
    @@ -154,14 +156,14 @@
    div.style.width = "150px";
    div.style.height = "200px";
    div.innerHTML = [
    "<p title=\""+item.title+"\">"+item.title.slice(0,20)+"</p>",
    "<p><img src=\""+item.thumbnail+"\"/></p>",
    "<p style=\"width: 130px; overflow: hidden;\">"+item.title+"</p>",
    "<p><img src=\""+item.thumbnail+"\" title=\""+item.title+"\"/></p>",
    "<p><a onclick=\"client.addNew('"+item.id+"',function(j){});return false;\" href=\"#\">キューに入れる</a></p>"
    ].join("");
    return div;
    };
    var updateUI = function(){
    var el = document.getElementById("play-history-pane");
    var el = document.getElementById("play-history-inner-pane");
    el.innerHTML = "";
    current_strategy.select(PlayLog.__log).forEach(function(item){
    el.appendChild(buildItemUI(item));
  2. ide-an revised this gist Mar 16, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion bgmsharehouse-playhistory.user.js
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,10 @@
    this.__select = f;
    }
    Strategy.prototype.select = function(history){
    return this.__select(history);
    //filter invalid log
    return this.__select(history.filter(function(v){
    return typeof(v.title) !== "undefined";
    }));
    };
    var current_strategy = null;
    var strategies = [
  3. ide-an revised this gist May 3, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion bgmsharehouse-playhistory.user.js
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,6 @@
    var res = [];
    for(var i=0;i<n;i++){
    var j = Math.floor(Math.random()*h.length);
    j = j===h.length ? j-1 : j;
    res.push(h[j]);
    h.splice(j,1);
    }
  4. ide-an revised this gist May 3, 2013. 1 changed file with 54 additions and 2 deletions.
    56 changes: 54 additions & 2 deletions bgmsharehouse-playhistory.user.js
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,43 @@
    return this.__log[index];
    }
    };
    var Strategy = function(name,f){
    this.name = name;
    this.__select = f;
    }
    Strategy.prototype.select = function(history){
    return this.__select(history);
    };
    var current_strategy = null;
    var strategies = [
    new Strategy("一番最近っぽい30件",
    function(history){
    var n = 30;
    if(history.length<n){
    return history.slice().reverse();
    }else{
    return history.slice(history.length - n).reverse();
    }
    }),
    new Strategy("一番昔っぽい30件",
    function(history){
    var n = 30;
    return history.slice(0, n);
    }),
    new Strategy("ランダムっぽい30件",
    function(history){
    var n = 30;
    var h = history.slice();
    var res = [];
    for(var i=0;i<n;i++){
    var j = Math.floor(Math.random()*h.length);
    j = j===h.length ? j-1 : j;
    res.push(h[j]);
    h.splice(j,1);
    }
    return res;
    })
    ];
    var injectPlayLogger = function(){
    var script = document.createElement("script");
    script.textContent = "("+(function(){
    @@ -86,19 +123,34 @@
    var initUI = function(){
    var div = document.createElement("div");
    div.innerHTML = [
    "<p>再生履歴</p>",
    '<div>再生履歴:<label for="play-history-strategy">表示</label><select id="play-history-strategy"></select></div>',
    '<div id="play-history-pane" ',
    'style="overflow: scroll;width: 100%;height: 10em;background-color: #fff;"',
    '>',
    '</div>'
    ].join("");
    var select = div.getElementsByTagName("select")[0];
    strategies.forEach(function(v,i){
    var option = document.createElement("option");
    option.value = i;
    option.textContent = v.name;
    select.appendChild(option);
    });
    var onchange = function(){
    var i = select.options[select.selectedIndex].value;
    current_strategy = strategies[i];
    updateUI();
    };
    select.addEventListener("change",onchange);
    document.getElementById("content-left").appendChild(div);
    onchange();
    };
    var buildItemUI = function(item){
    var div = document.createElement("div");
    div.style.position = "static";
    div.style.float = "left";
    div.style.width = "150px";
    div.style.height = "200px";
    div.innerHTML = [
    "<p title=\""+item.title+"\">"+item.title.slice(0,20)+"</p>",
    "<p><img src=\""+item.thumbnail+"\"/></p>",
    @@ -109,7 +161,7 @@
    var updateUI = function(){
    var el = document.getElementById("play-history-pane");
    el.innerHTML = "";
    PlayLog.__log.slice(0,10).forEach(function(item){
    current_strategy.select(PlayLog.__log).forEach(function(item){
    el.appendChild(buildItemUI(item));
    });
    };
  5. ide-an created this gist May 3, 2013.
    120 changes: 120 additions & 0 deletions bgmsharehouse-playhistory.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    // ==UserScript==
    // @include http://bgm.tokor.org/*
    // ==/UserScript==

    (function(){
    var PersistentStore = {
    read: function(key){
    return JSON.parse(window.localStorage.getItem(key));
    },
    write: function(key,val){
    return window.localStorage.setItem(key,JSON.stringify(val));
    }
    };

    var PlayLog = {
    __storeKey: "play_log",
    __log: [],
    load: function(){
    var log = PersistentStore.read(this.__storeKey);
    if(log){
    this.__log = log;
    }
    },
    store: function(){
    PersistentStore.write(this.__storeKey, this.__log);
    },
    add: function(info){
    if(!this.exist(info.id)){
    this.__log.push(info);
    }
    },
    find: function(key,val){
    for(var i=0,len=this.__log.length;i<len;i++){
    if(this.__log[i][key] === val){
    return i;
    }
    }
    return -1;
    },
    exist: function(id){
    return this.find("id",id) !== -1;
    },
    getInfo: function(index){
    return this.__log[index];
    }
    };
    var injectPlayLogger = function(){
    var script = document.createElement("script");
    script.textContent = "("+(function(){
    var fireEvent = function(name,val){
    var e;
    if(window.opera){
    e = document.createEvent("Event");
    e.initEvent(name, true, false);
    e.mydata = val;
    }else{
    e = document.createEvent("MessageEvent");
    e.initMessageEvent(name, true, false,
    JSON.stringify(val),
    location.protocol + "//" + location.host,
    "",
    window
    );
    }
    document.dispatchEvent(e);
    };
    onNextVideoLoaded = (function(f){
    return function(json){
    setTimeout(function(){
    fireEvent("playVideo",videoInfo);
    },1000);
    return f(json);
    };
    })(onNextVideoLoaded);
    }).toString()+")();";
    document.body.appendChild(script);
    };
    var initListener = function(){
    document.addEventListener("playVideo",function(e){
    var data = e.mydata || JSON.parse(e.data);
    PlayLog.add(data);
    PlayLog.store();
    updateUI();
    },false);
    };
    var initUI = function(){
    var div = document.createElement("div");
    div.innerHTML = [
    "<p>再生履歴</p>",
    '<div id="play-history-pane" ',
    'style="overflow: scroll;width: 100%;height: 10em;background-color: #fff;"',
    '>',
    '</div>'
    ].join("");
    document.getElementById("content-left").appendChild(div);
    };
    var buildItemUI = function(item){
    var div = document.createElement("div");
    div.style.position = "static";
    div.style.float = "left";
    div.style.width = "150px";
    div.innerHTML = [
    "<p title=\""+item.title+"\">"+item.title.slice(0,20)+"</p>",
    "<p><img src=\""+item.thumbnail+"\"/></p>",
    "<p><a onclick=\"client.addNew('"+item.id+"',function(j){});return false;\" href=\"#\">キューに入れる</a></p>"
    ].join("");
    return div;
    };
    var updateUI = function(){
    var el = document.getElementById("play-history-pane");
    el.innerHTML = "";
    PlayLog.__log.slice(0,10).forEach(function(item){
    el.appendChild(buildItemUI(item));
    });
    };
    PlayLog.load();
    initUI();
    initListener();
    injectPlayLogger();
    })();