InstaSynchP Wallcounter

Summarizes the lengths of each users video walls

当前为 2015-02-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        InstaSynchP Wallcounter
// @namespace   InstaSynchP
// @description Summarizes the lengths of each users video walls

// @version     1
// @author      Zod-
// @source      https://github.com/Zod-/InstaSynchP-Wallcounter
// @license     MIT

// @include     *://instasync.com/r/*
// @include     *://*.instasync.com/r/*
// @grant       none
// @run-at      document-start

// @require     https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=37716
// ==/UserScript==

function Wallcounter(version) {
  "use strict";
  this.version = version;
  this.name = 'InstaSynchP Wallcounter';
  this.counter = {};
  this.commands = {
    "'wallcounter": {
      'hasArguments': true,
      'reference': this,
      'description': 'Summarizes the lengths of each users video walls or specific users',
      'callback': this.execute
    }
  };
}

Wallcounter.prototype.resetVariables = function () {
  "use strict";
  this.counter = {};
};

Wallcounter.prototype.executeOnce = function () {
  "use strict";
  var th = this;

  cssLoader.add({
    'name': 'wallcounter',
    'url': 'https://cdn.rawgit.com/Zod-/InstaSynchP-Wallcounter/f234bce65b1fe913f13672cb8507dfa3dcf767d1/wallcounter.css',
    'autoload': true
  });

  function count(video, duration, inc) {
    var name = video.addedby.toLowerCase();
    if (th.counter.hasOwnProperty(name)) {
      //update wallcounter
      th.counter[name].duration += duration;
      th.counter[name].count += inc;
    } else {
      //create wallcounter
      th.counter[name] = {
        duration: duration,
        count: 1,
        origName: video.addedby
      };
    }
    //update own wallcounter
    if (name === thisUser().username.toLowerCase()) {
      $('#playlist_wallcounter').text(
        'Wallcounter[{1} - {0}]'.format(th.counter[name].count,
          window.utils.secondsToTime(th.counter[name].duration))
      );
    }
  }

  events.on(th, "AddVideo", function (video) {
    count(video, video.duration, +1);
  }, true);

  events.on(th, "RemoveVideo", function (ignore, video) {
    count(video, -video.duration, -1);
  }, true);
};

Wallcounter.prototype.preConnect = function () {
  "use strict";
  //add own wallcounter below the playlist
  $('.playlist-stats').append(
    $('<div>', {
      id: 'playlist_wallcounter'
    }).text('Wallcounter[00:00 - 0]')
  );
};

//let other plugins overwrite this
Wallcounter.prototype.formatOutput = function (counts) {
  "use strict";
  var output = "Wallcounter<br>";
  counts.forEach(function (count, index) {
    output += "{0}[<b>{2}</b> - {1}] - ".format(
      count.origName,
      count.count,
      utils.secondsToTime(count.duration)
    );
    //2 counters per line
    if ((index + 1) % 2 === 0) {
      //remove " - "
      output = output.substring(0, output.length - 3);
      output += '<br>';
    }
  });
  //remove " - "
  if (counts.length % 2 === 1) {
    output = output.substring(0, output.length - 3);
  }
  return output;
};

Wallcounter.prototype.execute = function (opts) {
  "use strict";
  var th = this,
    list = [];

  function addToList(username) {
    username = username.toLowerCase();
    if (!th.counter.hasOwnProperty(username) ||
      th.counter[username].count === 0) {
      return;
    }
    list.push(th.counter[username]);
  }

  function compareCount(c1, c2) {
    return c2.duration - c1.duration;
  }

  //collect users and their durations
  if (opts.usernames.length !== 0) {
    opts.usernames.forEach(addToList);
  } else {
    Object.keys(th.counter).forEach(addToList);
  }

  //sort by duration
  list.sort(compareCount);

  //display
  addSystemMessage(th.formatOutput(list));
};

window.plugins = window.plugins || {};
window.plugins.wallcounter = new Wallcounter('1');