InstaSynchP TrimWall Command

Trim a users wall down to the specified time

  1. // ==UserScript==
  2. // @name InstaSynchP TrimWall Command
  3. // @namespace InstaSynchP
  4. // @description Trim a users wall down to the specified time
  5.  
  6. // @version 1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-TrimWall-Command
  9. // @license MIT
  10.  
  11. // @include *://instasync.com/r/*
  12. // @include *://*.instasync.com/r/*
  13. // @grant none
  14. // @run-at document-start
  15.  
  16. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=37716
  17. // ==/UserScript==
  18.  
  19. function TrimWall(version) {
  20. "use strict";
  21. this.version = version;
  22. this.name = 'InstaSynchP TrimWall Command';
  23. this.settings = [{
  24. 'label': 'Default trim length (minutes)',
  25. 'id': 'trimwall-length',
  26. 'type': 'int',
  27. 'min': 0,
  28. 'default': 60,
  29. 'size': 8,
  30. 'section': ['Playlist', 'Trimwall']
  31. }];
  32. this.commands = {
  33. "'trimwall": {
  34. 'hasArguments': true,
  35. 'reference': this,
  36. 'description': 'Trim the wall of all or the specified users to a time limit.',
  37. 'callback': this.execute
  38. }
  39. };
  40. }
  41.  
  42. TrimWall.prototype.execute = function (opts) {
  43. "use strict";
  44. var th = this,
  45. map = {},
  46. maxTimeLimit = opts.numbers[0] || gmc.get('trimwall-length');
  47. maxTimeLimit *= 60;
  48.  
  49. //add all users when no user specified
  50. if(opts.usernames.length === 0){
  51. window.room.userlist.users.forEach(function(user){
  52. opts.usernames.push(user.username);
  53. });
  54. }
  55.  
  56. //initialize the map
  57. opts.usernames.forEach(function (user){
  58. map[user.toLowerCase()] = {
  59. videos: [],
  60. time: 0
  61. };
  62. });
  63.  
  64. //collect videos and durations
  65. window.room.playlist.videos.forEach(function(video){
  66. var key = video.addedby.toLowerCase();
  67. if(!map.hasOwnProperty(key)){
  68. return;
  69. }
  70. map[key].videos.push(video);
  71. map[key].time += video.duration;
  72. });
  73.  
  74. function cmpVidDur(v1, v2) {
  75. return v2.duration - v1.duration;
  76. }
  77.  
  78. for(var username in map){
  79. if(!map.hasOwnProperty(username)){
  80. continue;
  81. }
  82. //sort videos by length
  83. map[username].videos.sort(cmpVidDur);
  84. //remove till the video limit is hit
  85. for(var i = 0; i < map[username].videos.length && map[username].time > maxTimeLimit; i++){
  86. map[username].time -= map[username].videos[i].duration;
  87. sendcmd('remove',{
  88. info: map[username].videos[i].info
  89. });
  90. }
  91. }
  92. };
  93.  
  94. window.plugins = window.plugins || {};
  95. window.plugins.trimwall = new TrimWall('1');