Robin Enhancement Script

Highlight mentions, make link clickable, use channels & automatically remove spam

目前為 2016-04-05 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Robin Enhancement Script
  3. // @namespace https://www.reddit.com/
  4. // @version 3.1.0
  5. // @description Highlight mentions, make link clickable, use channels & automatically remove spam
  6. // @author Bag
  7. // @author netnerd01
  8. // @match https://www.reddit.com/robin*
  9. // @grant none
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13. (function() {
  14.  
  15. // Grab users username + play nice with RES
  16. var robin_user = $("#header-bottom-right .user a").first().text();
  17. var ignored_users = {};
  18.  
  19. // for spam counter - very important i know :P
  20. var blocked_spam_el = null;
  21. var blocked_spam = 0;
  22. var user_last_message = '';
  23. //
  24. var _robin_grow_detected = false;
  25.  
  26. /**
  27. * Pull tabber out in to semi-stand alone module
  28. * Big thanks to netnerd01 for his pre-work on this
  29. *
  30. * Basic usage - tabbedChannels.init( dom_node_to_add_tabs_to );
  31. * and hook up tabbedChannels.proccessLine(lower_case_text, jquery_of_line_container); to each line detected by the system
  32. */
  33. var tabbedChannels = new function(){
  34. var _self = this;
  35.  
  36. // Default options
  37. this.channels = ["~","*",".","%","$","#",";","^","<3",":gov","#rpg","@"];
  38. this.mode = 'single';
  39.  
  40. // internals
  41. this.unread_counts = {};
  42. this.$el = null;
  43. this.$opt = null;
  44. this.defaultRoomClasses = '';
  45. this.channelMatchingCache = [];
  46.  
  47. //channels user is in currently
  48. this.currentRooms = 0;
  49.  
  50. // When channel is clicked, toggle it on or off
  51. this.toggle_channel = function(e){
  52. var channel = $(e.target).data("filter");
  53. if(channel===null)return; // no a channel
  54.  
  55. if(!$("#robinChatWindow").hasClass("robin-filter-" + channel)){
  56. _self.enable_channel(channel);
  57. $(e.target).addClass("selected");
  58. // clear unread counter
  59. $(e.target).find("span").text(0);
  60. _self.unread_counts[channel] = 0;
  61. }else{
  62. _self.disable_channel(channel);
  63. $(e.target).removeClass("selected");
  64. }
  65.  
  66. // scroll everything correctly
  67. _scroll_to_bottom();
  68. };
  69.  
  70. // Enable a channel
  71. this.enable_channel = function(channel_id){
  72.  
  73. // if using room type "single", deslect other rooms on change
  74. if(this.mode == "single"){
  75. this.disable_all_channels();
  76. }
  77.  
  78. $("#robinChatWindow").addClass("robin-filter robin-filter-" + channel_id);
  79. $("#robinChatWindow").attr("data-channel-key", this.channels[channel_id]);
  80. this.currentRooms++;
  81. // unselect show all
  82. _self.$el.find("span.all").removeClass("selected");
  83. };
  84.  
  85. // disable a channel
  86. this.disable_channel = function(channel_id){
  87. $("#robinChatWindow").removeClass("robin-filter-" + channel_id);
  88. this.currentRooms--;
  89.  
  90. // no rooms selcted, run "show all"
  91. if(this.currentRooms == 0) this.disable_all_channels();
  92. };
  93.  
  94. // turn all channels off
  95. this.disable_all_channels = function(e){
  96. $("#robinChatWindow").attr("class", _self.defaultRoomClasses);
  97. _self.$el.find(".robin-filters > span").removeClass("selected");
  98. this.currentRooms = 0;
  99.  
  100. _self.$el.find("span.all").addClass("selected");
  101. _scroll_to_bottom();
  102. };
  103.  
  104. // render tabs
  105. this.drawTabs = function(){
  106. html = '';
  107. for(var i in this.channels){
  108. if(typeof this.channels[i] === 'undefined') continue;
  109. html += '<span data-filter="' + i + '" data-filter-name="'+ this.channels[i] +'">' + this.channels[i] + ' (<span>0</span>)</span> ';
  110. }
  111. this.$el.find(".robin-filters").html(html);
  112. };
  113.  
  114. // Add new channel
  115. this.addChannel = function(new_channel){
  116. if(this.channels.indexOf(new_channel) === -1){
  117. this.channels.push(new_channel);
  118. this.unread_counts[this.channels.length-1] = 0;
  119. this.updateChannelMatchCache();
  120. this.saveChannelList();
  121. this.drawTabs();
  122.  
  123. // refresh everything after redraw
  124. this.disable_all_channels();
  125. }
  126. };
  127.  
  128. // remove existing channel
  129. this.removeChannel = function(channel){
  130. if(confirm("are you sure you wish to remove the " + channel + " channel?")){
  131. var idx = this.channels.indexOf(channel);
  132. delete this.channels[idx];
  133. this.updateChannelMatchCache();
  134. this.saveChannelList();
  135. this.drawTabs();
  136. // refresh everything after redraw
  137. this.disable_all_channels();
  138. }
  139. };
  140.  
  141.  
  142. // save channel list
  143. this.saveChannelList = function(){
  144. // clean array before save
  145. var channels = this.channels.filter(function (item) { return item != undefined });
  146. GM_setValue("robin-enhance-channels", channels);
  147. };
  148.  
  149. // Change chat mode
  150. this.changeChannelMode = function(e){
  151. _self.mode = $(this).data("type");
  152.  
  153. // swicth bolding
  154. $(this).parent().find("span").css("font-weight","normal");
  155. $(this).css("font-weight","bold");
  156. _self.disable_all_channels();
  157.  
  158. // Update mode setting
  159. GM_setValue("robin-enhance-mode", _self.mode);
  160. };
  161.  
  162. this.updateChannelMatchCache = function(){
  163. var order = this.channels.slice(0);
  164. order.sort(function(a, b){
  165. return b.length - a.length; // ASC -> a - b; DESC -> b - a
  166. });
  167. for(var i in order){
  168. order[i] = this.channels.indexOf(order[i]);
  169. }
  170. // sorted array of channel name indexs
  171.  
  172. this.channelMatchingCache = order;
  173. }
  174.  
  175. // Procces each chat line to create text
  176. this.proccessLine = function(text, $element){
  177. var i, idx, channel;
  178. for(i=0; i< this.channelMatchingCache.length; i++){
  179. idx = this.channelMatchingCache[i];
  180. channel = this.channels[idx];
  181.  
  182. if(typeof channel === 'undefined') continue;
  183.  
  184. if(text.indexOf(channel) === 0){
  185. $element.addClass("robin-filter-" + idx +" in-channel");
  186. this.unread_counts[idx]++;
  187. return;
  188. }
  189. }
  190. };
  191.  
  192. // If in one channel, auto add channel keys
  193. this.submit_helper = function(){
  194. if($("#robinChatWindow").hasClass("robin-filter")){
  195. // auto add channel key
  196. var channel_key = $("#robinChatWindow").attr("data-channel-key");
  197.  
  198. if($(".text-counter-input").val().indexOf("/me") === 0){
  199. $(".text-counter-input").val("/me " + channel_key + " " + $(".text-counter-input").val().substr(3));
  200. }else if($(".text-counter-input").val().indexOf("/") !== 0){
  201. // if its not a "/" command, add channel
  202. $(".text-counter-input").val(channel_key + " " + $(".text-counter-input").val());
  203. }
  204. }
  205. };
  206.  
  207. // Update everuything
  208. this.tick = function(){
  209. _self.$el.find(".robin-filters span").each(function(){
  210. if($(this).hasClass("selected")) return;
  211. $(this).find("span").text(_self.unread_counts[$(this).data("filter")]);
  212. });
  213. };
  214.  
  215. // Init tab zone
  216. this.init = function($el){
  217. // Load channels
  218. if(GM_getValue("robin-enhance-channels")){
  219. this.channels = GM_getValue("robin-enhance-channels");
  220. }
  221. if(GM_getValue("robin-enhance-mode")){
  222. this.mode = GM_getValue("robin-enhance-mode");
  223. }
  224.  
  225. // init counters
  226. for(var i in this.channels){
  227. this.unread_counts[i] = 0;
  228. }
  229.  
  230. // update channel cache
  231. this.updateChannelMatchCache();
  232.  
  233. // set up el
  234. this.$el = $el;
  235.  
  236. // Create inital markup
  237. this.$el.html("<span class='all selected'>Everything</span><span><div class='robin-filters'></div></span><span class='more'>[Options]</span>");
  238. this.$opt = $("<div class='robin-channel-add' style='display:none'><input name='add-channel'><button>Add channel</button> <span class='channel-mode'>Channel Mode: <span title='View one channel at a time' data-type='single'>Single</span> | <span title='View many channels at once' data-type='multi'>Multi</span></span></div>").insertAfter(this.$el);
  239.  
  240. // Attach events
  241. this.$el.find(".robin-filters").click(this.toggle_channel);
  242. this.$el.find("span.all").click(this.disable_all_channels);
  243. this.$el.find("span.more").click(function(){ $(".robin-channel-add").slideToggle(); });
  244. this.$el.find(".robin-filters").bind("contextmenu", function(e){
  245. e.preventDefault();
  246. e.stopPropagation();
  247. var chan_id = $(e.target).data("filter");
  248. if(chan_id===null)return; // no a channel
  249. _self.removeChannel(_self.channels[chan_id]);
  250. });
  251. // Form events
  252. this.$opt.find(".channel-mode span").click(this.changeChannelMode);
  253. this.$opt.find("button").click(function(){
  254. var new_chan = _self.$opt.find("input[name='add-channel']").val();
  255. if(new_chan != '') _self.addChannel(new_chan);
  256. _self.$opt.find("input[name='add-channel']").val('');
  257. });
  258.  
  259. $("#robinSendMessage").submit(this.submit_helper);
  260. // store default room class
  261. this.defaultRoomClasses = $("#robinChatWindow").attr("class");
  262.  
  263. // redraw tabs
  264. this.drawTabs();
  265.  
  266. // start ticker
  267. setInterval(this.tick, 1000);
  268. }
  269. };
  270.  
  271. /**
  272. * Check if a message is "spam"
  273. */
  274. var is_spam = function(line){
  275. return (
  276. // Hide auto vote messages
  277. (/^voted to (grow|stay|abandon)/.test(line)) ||
  278. // random unicode?
  279. (/[\u0080-\uFFFF]/.test(line)) ||
  280. // hide any auto voter messages
  281. (/\[.*autovoter.*\]/.test(line)) ||
  282. // Common bots
  283. (/^(\[binbot\]|\[robin-grow\])/.test(line)) ||
  284. // repeating chars in line (more than 5). e.g. aaaaaaa !!!!!!!!
  285. (/(.)\1{5,}/.test(line)) ||
  286. // Some common messages
  287. (/(voting will end in approximately|\[i spam the most used phrase\]|\[message from creator\]|\[.*bot.*\])/.test(line)) ||
  288. // no spaces = spam if its longer than 25 chars (dont filter links)
  289. (line.indexOf(" ") === -1 && line.length > 25 && line.indexOf("http") === -1) ||
  290. // repeating same word
  291. /(\b\S+\b)\s+\b\1\b/i.test(line)
  292. );
  293. };
  294.  
  295. /**
  296. * Check if a message is from an ignored user
  297. *
  298. */
  299. var is_ignored = function($usr, $ele){
  300. // no user name, go looking for when said it
  301. if($usr.length === 0){
  302. while($usr.length === 0){
  303. $ele = $ele.prev();
  304. $usr = $ele.find(".robin--username");
  305. }
  306. }
  307. // are they ignored?
  308. return (ignored_users[$usr.text()]);
  309. };
  310.  
  311. /**
  312. * Make links clickable
  313. *
  314. */
  315. var auto_link = function($msg){
  316. var text = $msg.html(); // read as html so stuff stays escaped
  317. // normal links
  318. text = text.replace(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim, '<a target="blank" href="$&">$&</a>');
  319. // reddit subreddit links
  320. text = text.replace(/ \/r\/(\w+)/gim, ' <a target="blank" href="https://reddit.com/r/$1">/r/$1</a>');
  321. // update text
  322. $msg.html(text);
  323. };
  324.  
  325. /**
  326. * Mute a user
  327. */
  328. var _mute_user = function(usr){
  329. // Add to ignore list
  330. ignored_users[usr] = true;
  331. _render_muted_list();
  332. };
  333.  
  334. /**
  335. * un-mute a user
  336. */
  337. var _unmute_user = function(usr){
  338. // Add to ignore list
  339. delete ignored_users[usr];
  340. _render_muted_list();
  341. };
  342.  
  343. // Render list of ignored users
  344. var _render_muted_list = function(){
  345. var html = "<strong>Ignored users</strong><br>";
  346. for(var u in ignored_users){
  347. html += "<div data-usr='"+ u + "'>" + u + " - [unmute]</div>";
  348. }
  349. $("#muted_users").html(html);
  350. };
  351.  
  352. // Scroll chat back to bottom
  353. var _scroll_to_bottom = function(){
  354. $("#robinChatWindow").scrollTop($("#robinChatMessageList").height());
  355. };
  356.  
  357. var update_spam_count = function(){
  358. blocked_spam++;
  359. blocked_spam_el.innerHTML = blocked_spam;
  360. };
  361.  
  362. var fill_name = function(e){
  363. e.preventDefault();
  364. e.stopPropagation();
  365.  
  366. // if text area blank, prefill name. if not, stick it on the end
  367. if($(".text-counter-input").val() === ''){
  368. $(".text-counter-input").val($(this).text() + ' ').focus();
  369. }else{
  370. $(".text-counter-input").val($(".text-counter-input").val() + ' ' + $(this).text()).focus();
  371. }
  372. };
  373.  
  374. /**
  375. * Parse a link and apply changes
  376. */
  377. var parse_line = function($ele){
  378. var $msg = $ele.find(".robin-message--message");
  379. var $usr = $ele.find(".robin--username");
  380. var line = $msg.text().toLowerCase();
  381. // dont parse system messages
  382. if($ele.hasClass("robin--user-class--system")){
  383. if(line.indexOf("ratelimit | you are doing that too much") !== -1){
  384. $(".text-counter-input").val(user_last_message);
  385. }
  386. return;
  387. }
  388.  
  389. // If user is ignored or message looks like "Spam". hide it
  390. if (is_ignored($usr, $ele) || is_spam(line)) {
  391. $ele.addClass("spam-hidden");
  392. update_spam_count();
  393. }
  394.  
  395. // Highlight mentions
  396. if(line.indexOf(robin_user) !== -1){
  397. $ele.addClass("user-mention");
  398. }
  399.  
  400. // Make links clickable
  401. if(!_robin_grow_detected && line.indexOf("http") !== -1){
  402. auto_link($msg);
  403. }
  404.  
  405. // Add mute button to users
  406. if(!$ele.hasClass("robin--user-class--system") && $usr.text() != robin_user){
  407. $("<span style='font-size:.8em;cursor:pointer'> [mute] </span>").insertBefore($usr).click(function(){
  408. _mute_user($usr.text());
  409. });
  410. }
  411.  
  412. // Track channels
  413. tabbedChannels.proccessLine(line, $ele);
  414.  
  415. // bind click to use (override other click events if we can)
  416. $usr.bindFirst("click", fill_name);
  417. };
  418.  
  419.  
  420. // Detect changes, are parse the new message
  421. $("#robinChatWindow").on('DOMNodeInserted', function(e) {
  422. if ($(e.target).is('div.robin-message')) {
  423. // Apply changes to line
  424. parse_line($(e.target));
  425. }
  426. });
  427.  
  428. // When everything is ready
  429. $(document).ready(function(){
  430.  
  431. // Set default spam filter type
  432. $("#robinChatWindow").addClass("hide-spam");
  433.  
  434. // Add checkbox to toggle "hide" behaviors
  435. $("#robinDesktopNotifier").append("<label><input type='checkbox' checked='checked'>Hide spam completely (<span id='spamcount'>0</span> removed)</label>").click(function(){
  436. if($(this).find("input").is(':checked')){
  437. $("#robinChatWindow").removeClass("mute-spam").addClass("hide-spam");
  438. }else{
  439. $("#robinChatWindow").removeClass("hide-spam").addClass("mute-spam");
  440. }
  441. // correct scroll after spam filter change
  442. _scroll_to_bottom();
  443. });
  444.  
  445. blocked_spam_el = $("#spamcount")[0];
  446.  
  447. // Add Muted list & hook up unmute logic
  448. $('<div id="muted_users" class="robin-chat--sidebar-widget robin-chat--notification-widget"><strong>Ignored users</strong></div>').insertAfter($("#robinDesktopNotifier"));
  449. $('#muted_users').click(function(e){
  450. var user = $(e.target).data("usr");
  451. if(user) _unmute_user(user);
  452. });
  453.  
  454. // Init tabbed channels
  455. tabbedChannels.init($('<div id="filter_tabs"></div>').insertAfter("#robinChatWindow"));
  456.  
  457. // store i copy of last message, in case somthing goes wrong (rate limit)
  458. $("#robinSendMessage").submit(function(){
  459. user_last_message = $(".text-counter-input").val();
  460. });
  461.  
  462. });
  463.  
  464. // fix by netnerd01
  465. var stylesheet = document.createElement('style');
  466. document.head.appendChild(stylesheet);
  467. stylesheet = stylesheet.sheet;
  468.  
  469. // filter for channel
  470. stylesheet.insertRule("#robinChatWindow.robin-filter div.robin-message { display:none; }", 0);
  471. stylesheet.insertRule("#robinChatWindow.robin-filter div.robin-message.robin--user-class--system { display:block; }", 0);
  472. for(var c=0;c<35;c++){
  473. stylesheet.insertRule("#robinChatWindow.robin-filter.robin-filter-"+c+" div.robin-message.robin-filter-"+c+" { display:block; }", 0);
  474. }
  475.  
  476. // Styles for filter tabs
  477. stylesheet.insertRule("#filter_tabs {width:100%; display: table; table-layout: fixed; background:#d7d7d2; border-bottom:1px solid #efefed;}",0);
  478. stylesheet.insertRule("#filter_tabs > span {width:90%; display: table-cell;}",0);
  479. stylesheet.insertRule("#filter_tabs > span.all, #filter_tabs > span.more {width:60px; text-align:center; vertical-align:middle; cursor:pointer;}",0);
  480. stylesheet.insertRule("#filter_tabs > span.all.selected, #filter_tabs > span.all.selected:hover {background: #fff;}", 0);
  481. stylesheet.insertRule("#filter_tabs .robin-filters { display: table; width:100%;table-layout: fixed; '}", 0);
  482. stylesheet.insertRule("#filter_tabs .robin-filters > span { padding: 5px 2px;text-align: center; display: table-cell; cursor: pointer;width:2%; vertical-align: middle; font-size: 1.1em;}", 0);
  483. stylesheet.insertRule("#filter_tabs .robin-filters > span.selected, #filter_tabs .robin-filters > span:hover { background: #fff;}", 0);
  484. stylesheet.insertRule("#filter_tabs .robin-filters > span > span {pointer-events: none;}", 0);
  485.  
  486. stylesheet.insertRule(".robin-channel-add {padding:5px; display:none;}", 0);
  487. stylesheet.insertRule(".robin-channel-add input {padding: 2.5px; }", 0);
  488. stylesheet.insertRule(".robin-channel-add .channel-mode {float:right; font-size:1.2em;padding:5px;}", 0);
  489. stylesheet.insertRule(".robin-channel-add .channel-mode span {cursor:pointer}", 0);
  490. //mentions should show even in filter view
  491. stylesheet.insertRule("#robinChat #robinChatWindow div.robin-message.user-mention { display:block; font-weight:bold; }", 0);
  492.  
  493. // Add initial styles for "spam" messages
  494. stylesheet.insertRule("#robinChat #robinChatWindow.hide-spam div.robin-message.spam-hidden { display:none; }", 0);
  495. stylesheet.insertRule("#robinChat #robinChatWindow.mute-spam div.robin-message.spam-hidden { opacity:0.3; font-size:1.2em; }", 0);
  496.  
  497. // muted user box
  498. stylesheet.insertRule("#muted_users { font-size:1.2em; }", 0);
  499. stylesheet.insertRule("#muted_users div { padding: 2px 0; }", 0);
  500. stylesheet.insertRule("#muted_users strong { font-weight:bold; }", 0);
  501.  
  502. // FIX RES nightmode (ish) [ by Kei ]
  503. stylesheet.insertRule(".res-nightmode #robinChatWindow div.robin-message { color: #ccc; }", 0);
  504. stylesheet.insertRule(".res-nightmode .robin-chat--sidebar-widget { background: #222; color: #ccc;}", 0);
  505. stylesheet.insertRule(".res-nightmode .robin-room-participant { background: #222; color: #999;}", 0);
  506. stylesheet.insertRule(".res-nightmode #filter_tabs {background: rgb(51, 51, 51);}", 0);
  507. stylesheet.insertRule(".res-nightmode #filter_tabs .robin-filters > span.selected,.res-nightmode #filter_tabs .robin-filters > span:hover,.res-nightmode #filter_tabs > span.all.selected,.res-nightmode #filter_tabs > span.all:hover {background: rgb(34, 34, 34)}", 0);
  508. stylesheet.insertRule(".res-nightmode .robin-chat--input { background: #222 }", 0);
  509. stylesheet.insertRule(".res-nightmode .robin--presence-class--away .robin--username {color: #999;}", 0);
  510. stylesheet.insertRule(".res-nightmode .robin--presence-class--present .robin--username {color: #ccc;}", 0);
  511. stylesheet.insertRule(".res-nightmode #robinChat .robin--user-class--self .robin--username { color: #999; }", 0);
  512. stylesheet.insertRule(".res-nightmode .robin-chat--vote { background: #777; color: #ccc;}", 0);
  513. stylesheet.insertRule(".res-nightmode .robin-chat--buttons button.robin-chat--vote.robin--active { background: #ccc; color:#999; }", 0);
  514.  
  515. $(document).ready(function(){
  516. setTimeout(function(){
  517. // Play nice with robin grow (makes room for tab bar we insert)
  518. if($(".usercount.robin-chat--vote").length !== 0){
  519. _robin_grow_detected = true;
  520. stylesheet.insertRule("#robinChat.robin-chat .robin-chat--body { height: calc(100vh - 150px); }", 0);
  521. }
  522. },500);
  523. });
  524.  
  525. // Allow me to sneek functions in front of other libaries - used when working with robin grow >.< sorry guys
  526. //http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t
  527. $.fn.bindFirst = function(name, fn) {
  528. // bind as you normally would
  529. // don't want to miss out on any jQuery magic
  530. this.on(name, fn);
  531.  
  532. // Thanks to a comment by @Martin, adding support for
  533. // namespaced events too.
  534. this.each(function() {
  535. var handlers = $._data(this, 'events')[name.split('.')[0]];
  536. // take out the handler we just inserted from the end
  537. var handler = handlers.pop();
  538. // move it at the beginning
  539. handlers.splice(0, 0, handler);
  540. });
  541. };
  542.  
  543. })();