Disqus Click Automate

Automate repetitive clicks on those sites that use Disqus as commenting system.

目前为 2015-05-09 提交的版本。查看 最新版本

  1. // @grant GM_setValue
  2. // @grant GM_getValue
  3. // @grant GM_registerMenuCommand
  4. // ==UserScript==
  5. // @name Disqus Click Automate
  6. // @autor aaferrari@eml.cc, updated by flatline4400@gmail.com (thanks woxxom at greasyfork)
  7. // @namespace Disqus Click Automate
  8. // @description Automate repetitive clicks on those sites that use Disqus as commenting system.
  9. // @include *
  10. // @version 0.3
  11. // ==/UserScript==
  12.  
  13. // This open a dialog to customize some parameters of the script, add the click events in
  14. // the respective radioboxs, checkboxes and set the way that this dialogue disappears from
  15. // the page when the user wants to close
  16. function config_dialog(){
  17. var i18n = {
  18. "en": {
  19. "SORTING_LABEL": "Sort comments by",
  20. "RECENT_LABEL": "Most recent",
  21. "BEST_LABEL": "The best",
  22. "OLDEST_LABEL": "Oldest",
  23. "AUTOLOAD_LABEL": "Autoload all comments",
  24. "EXPAND_LABEL": "Expand shortened comments",
  25. "GUEST_LABEL": "I am not interested in log in/create an account, I just want to comment",
  26. "CLOSE_TITLE": "Close",
  27. "AUTOLOAD_TITLE": "Load all comments available automatically instead of showing the 50 first",
  28. "EXPAND_TITLE": 'Expand automatically shortened comment containing the link "See more"',
  29. "GUEST_TITLE": 'Check the option "I prefer commenting as guest" and hidden the password field on the comment form'
  30. },
  31. "es": {
  32. "SORTING_LABEL": "Ordenar comentarios por",
  33. "RECENT_LABEL": "Mas nuevos",
  34. "BEST_LABEL": "El mejor",
  35. "OLDEST_LABEL": "Mas antiguos",
  36. "AUTOLOAD_LABEL": "Autocargar todos los comentarios",
  37. "EXPAND_LABEL": "Expandir comentarios acortados",
  38. "GUEST_LABEL": "No me interesa iniciar sesión/crear una cuenta, solo quiero comentar",
  39.  
  40. "CLOSE_TITLE": "Cerrar",
  41. "AUTOLOAD_TITLE": "Carga automáticamente todos los comentarios disponibles en vez de mostrar los 50 primeros",
  42. "EXPAND_TITLE": 'Expande de forma automática los comentarios acortados que contienen el enlace "Ver más"',
  43. "GUEST_TITLE": 'Marca la opcion "Prefiero comentar como invitado" y oculta el campo de contraseña en el formulario para comentar'
  44. }
  45. }
  46.  
  47. function translate(string){
  48. var lang = navigator.userLanguage || navigator.language;
  49. if (i18n[lang]) {
  50. if (i18n[lang][string]) { return i18n[lang][string]; }
  51. }
  52. return i18n["en"][string];
  53. }
  54.  
  55. // Changed the inline html to Javascript DOM (special thanks to
  56. // http://rick.measham.id.au/paste/html2dom.htm for saving so much work) to
  57. // avoid problems with the Content-Security-Policy on pages that have embedded
  58. // the Disqus comments, as well as other sites that also possess these policies.
  59. configDialogStyle = document.createElement('style');
  60. configDialogStyle.id = "config-dialog-style";
  61. configDialogStyle.appendChild(document.createTextNode("\
  62. #div_Content{position: fixed;\
  63. top: 50%; left: 50%;\
  64. transform: translate(-50%, -50%);\
  65. -ms-transform:translate(-50%, -50%); /* IE 9 */\
  66. webkit-transform:translate(-50%, -50%); /* Safari and Chrome */\
  67. z-index: 999; background-color: #ffffff;\
  68. border-color: black; border: 2px solid #000;}\
  69. #div_Content:focus{display:none;}\
  70. .dsq-options li {float: left; margin: 0px 5px;}"));
  71. document.body.appendChild(configDialogStyle);
  72.  
  73. div_Content = document.createElement('div');
  74. div_Content.id = "div_Content";
  75.  
  76. var table_0 = document.createElement('table');
  77. table_0.style.textAlign = "left";
  78.  
  79. var tbody_0 = document.createElement('tbody');
  80.  
  81. var tr_0 = document.createElement('tr');
  82. tr_0.style.color = "white";
  83.  
  84. var td_0 = document.createElement('td');
  85. td_0.style.verticalAlign = "top";
  86. td_0.style.textAlign = "right";
  87.  
  88. var btnClose = document.createElement('span');
  89. btnClose.style.background = "red none repeat scroll 0% 50%";
  90. btnClose.style.cursor = "pointer";
  91. btnClose.style.fontWeight = "bold";
  92. btnClose.style.MozBackgroundClip = "-moz-initial";
  93. btnClose.style.MozBackgroundOrigin = "-moz-initial";
  94. btnClose.style.MozBackgroundInlinePolicy = "-moz-initial";
  95. btnClose.id = "btn close";
  96. btnClose.title = translate("CLOSE_TITLE");
  97. btnClose.innerHTML = " X ";
  98.  
  99. td_0.appendChild(btnClose);
  100.  
  101. tr_0.appendChild(td_0);
  102.  
  103. tbody_0.appendChild(tr_0);
  104.  
  105. var tr_1 = document.createElement('tr');
  106.  
  107. var td_1 = document.createElement('td');
  108. td_1.appendChild(document.createTextNode(translate("SORTING_LABEL") + ":"));
  109.  
  110. tr_1.appendChild(td_1);
  111.  
  112. tbody_0.appendChild(tr_1);
  113.  
  114. var tr_2 = document.createElement('tr');
  115.  
  116. var td_2 = document.createElement('td');
  117.  
  118. var ul_0 = document.createElement('ul');
  119. ul_0.style.listStyle = "none outside none";
  120. ul_0.className = "dsq-options";
  121.  
  122. var li_0 = document.createElement('li');
  123.  
  124. var dsq_sorting_desc = document.createElement('input');
  125. dsq_sorting_desc.name = "sorting";
  126. dsq_sorting_desc.type = "radio";
  127. dsq_sorting_desc.id = "dsq-sorting-desc";
  128. li_0.appendChild(dsq_sorting_desc);
  129.  
  130. li_0.appendChild(document.createTextNode(translate("RECENT_LABEL")));
  131. ul_0.appendChild(li_0);
  132.  
  133. var li_1 = document.createElement('li');
  134.  
  135. var dsq_sorting_popular = document.createElement('input');
  136. dsq_sorting_popular.name = "sorting";
  137. dsq_sorting_popular.type = "radio";
  138. dsq_sorting_popular.id = "dsq-sorting-popular";
  139. li_1.appendChild(dsq_sorting_popular);
  140.  
  141. li_1.appendChild(document.createTextNode(translate("BEST_LABEL")));
  142. ul_0.appendChild(li_1);
  143.  
  144. var li_2 = document.createElement('li');
  145.  
  146. var dsq_sorting_asc = document.createElement('input');
  147. dsq_sorting_asc.name = "sorting";
  148. dsq_sorting_asc.type = "radio";
  149. dsq_sorting_asc.id = "dsq-sorting-asc";
  150. li_2.appendChild(dsq_sorting_asc);
  151.  
  152. li_2.appendChild(document.createTextNode(translate("OLDEST_LABEL")));
  153. ul_0.appendChild(li_2);
  154.  
  155. td_2.appendChild(ul_0);
  156.  
  157. tr_2.appendChild(td_2);
  158.  
  159. tbody_0.appendChild(tr_2);
  160.  
  161. var tr_3 = document.createElement('tr');
  162.  
  163. var td_3 = document.createElement('td');
  164.  
  165. var br_2 = document.createElement('br');
  166. td_3.appendChild(br_2);
  167.  
  168. tr_3.appendChild(td_3);
  169.  
  170. tbody_0.appendChild(tr_3);
  171.  
  172. var tr_4 = document.createElement('tr');
  173.  
  174. var td_4 = document.createElement('td');
  175. td_4.title = translate("AUTOLOAD_TITLE");
  176.  
  177. var autoload = document.createElement('input');
  178. autoload.type = "checkbox";
  179. autoload.id = "autoload_comments";
  180. autoload.title = translate("AUTOLOAD_TITLE");
  181. td_4.appendChild(autoload);
  182.  
  183. td_4.appendChild(document.createTextNode(translate("AUTOLOAD_LABEL")));
  184. tr_4.appendChild(td_4);
  185.  
  186. tbody_0.appendChild(tr_4);
  187.  
  188.  
  189. var tr_5 = document.createElement('tr');
  190.  
  191. var td_5 = document.createElement('td');
  192. td_5.title = translate("EXPAND_TITLE");
  193.  
  194. var autoexpand = document.createElement('input');
  195. autoexpand.type = "checkbox";
  196. autoexpand.id = "autoexpand_comments";
  197. autoexpand.title = translate("EXPAND_TITLE");
  198. td_5.appendChild(autoexpand);
  199.  
  200. td_5.appendChild(document.createTextNode(translate("EXPAND_LABEL")));
  201. tr_5.appendChild(td_5);
  202.  
  203. tbody_0.appendChild(tr_5);
  204.  
  205. var tr_6 = document.createElement('tr');
  206.  
  207. var td_6 = document.createElement('td');
  208. td_6.title = translate("GUEST_TITLE");
  209.  
  210. var without_login = document.createElement('input');
  211. without_login.type = "checkbox";
  212. without_login.id = "guest-comment";
  213. without_login.title = translate("GUEST_TITLE");
  214. td_6.appendChild(without_login);
  215.  
  216. td_6.appendChild(document.createTextNode(translate("GUEST_LABEL")));
  217. tr_6.appendChild(td_6);
  218. tbody_0.appendChild(tr_6);
  219.  
  220. table_0.appendChild(tbody_0);
  221.  
  222. div_Content.appendChild(table_0);
  223.  
  224. document.body.appendChild(div_Content);
  225.  
  226. // This function is only added for the purpose of facilitate the process of
  227. // inserting click events in radiobuttons and checkboxes (both current and
  228. // new) of the dialog config
  229. function click_check_setter(element, gm_value, pseudo_global_var, getted_value){
  230. var superior_scope = this; //Necessary to modify the "pseudo_global_var" from click event
  231. create_click_event(element, function () {
  232. GM_setValue(gm_value, getted_value === "" ? element.checked: getted_value);
  233. superior_scope[pseudo_global_var] = getted_value === "" ? element.checked: getted_value;
  234. });
  235. }
  236. // Init config dialog
  237. var sorting = {"desc": dsq_sorting_desc, "popular": dsq_sorting_popular, "asc": dsq_sorting_asc};
  238. sorting[default_order].checked= true;
  239. autoload.checked = autoload_comments;
  240. click_check_setter(autoload, 'autoload', "autoload_comments", "");
  241. autoexpand.checked = expand_comments;
  242. click_check_setter(autoexpand, 'expand', "expand_comments", "");
  243. without_login.checked = guest_comment;
  244. click_check_setter(without_login, 'guest', "guest_comment", "");
  245. // Set onclick event in radiobuttons
  246. click_check_setter(dsq_sorting_desc, 'sorting', "default_order", "desc");
  247. click_check_setter(dsq_sorting_popular, 'sorting', "default_order", "popular");
  248. click_check_setter(dsq_sorting_asc, 'sorting', "default_order", "asc");
  249. // Click event to close de config dialog
  250. create_click_event(document, function (e) {
  251. e = e || event
  252. var target = e.target || e.srcElement
  253. do {
  254. if (div_Content == target) {
  255. // Click occured inside the box, do nothing.
  256. return
  257. }
  258. target = target.parentNode
  259. } while (target)
  260. // Click was outside the box, destroy it.
  261. del_element(div_Content);
  262. del_element(configDialogStyle);
  263. });
  264.  
  265. create_click_event(btnClose, function(){
  266. del_element(div_Content);
  267. del_element(configDialogStyle);
  268. });
  269. }
  270.  
  271. function clicker(link){
  272. if (link.click) {link.click();}
  273. else{
  274. var new_event = document.createEvent("MouseEvents");
  275. new_event.initMouseEvent("click", true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
  276. try { link.dispatchEvent(new_event); }
  277. // Do nothing in this catch, this is a problem with JQuery compatibility, more details in
  278. // http://stackoverflow.com/questions/980697/element-dispatchevent-is-not-a-function-js-error-caught-in-firebug-of-ff3-0
  279. catch(error) {}
  280. }
  281. }
  282.  
  283. function finder(tag, attrib, value, one_element){
  284. if (document.querySelector || document.querySelectorAll){
  285. var selectors = {true: "querySelector", false: "querySelectorAll"}
  286. return document[selectors[one_element]](tag + '['+ attrib+ '="'+ value + '"]');
  287. }
  288. else {
  289. var elems = document.getElementsByTagName(tag);
  290. var results = Array();
  291. for (i in elems){
  292. if(elems[i].getAttribute(attrib) == value) {
  293. if (one_element == true) { return elems[i]; }
  294. else { results.push(elems[i]); }
  295. }
  296. }
  297. return results;
  298. }
  299. }
  300.  
  301. function is_class_name(elem, matchClass){
  302. //Adapted from http://stackoverflow.com/a/3808886
  303. if (elem == null) { return null; }
  304. else if (typeof(matchClass) == 'string') {
  305. return (' ' + elem.className + ' ').indexOf(' ' + matchClass + ' ') > -1;
  306. }
  307. else if (matchClass instanceof RegExp) { return matchClass.test(elem.className); }
  308. }
  309.  
  310. function create_click_event(element, func) {
  311. if (element.addEventListener) {
  312. element.addEventListener("click", func, false); }
  313. else { elemen.attachEvent("onclick", func); } //Internet Explorer
  314. }
  315.  
  316. function del_element(id){
  317. if (typeof(id) == "string") { var element = document.getElementById(id); }
  318. else { element = id; }
  319. if (element){
  320. var ancestry = element.parentNode;
  321. ancestry.removeChild(element);
  322. }
  323. }
  324.  
  325. //saved values
  326. default_order = GM_getValue("sorting", "desc");
  327. autoload_comments = GM_getValue("autoload", true);
  328. expand_comments = GM_getValue("expand", true);
  329. guest_comment = GM_getValue("guest", true);
  330.  
  331. //register GM menu command
  332. GM_registerMenuCommand("[Disqus Click Automate] Configuration", config_dialog);
  333.  
  334. // This line (with the @include *) is needed to open the configuration dialog at any time from any page
  335. if (window.location.href.search("disqus.com/embed/comments") == -1 ) {return;}
  336.  
  337. all_comments_retrieved = false;
  338. //var JSON_Disqus = eval("("+document.getElementById("disqus-jsonData").innerHTML+")");
  339. //var actual_comments = JSON_Disqus["response"]["posts"].length;
  340. total_comments = null
  341. comments = null
  342.  
  343. // Ordering comments and get some extra data
  344. sorted_comments = false;
  345. initial_timer = window.setInterval(function (){
  346. var actual_sort = finder("li", 'class', "selected", true);
  347. if (actual_sort != null) {
  348. var choosen_sort = finder("a", 'data-sort', default_order, true);
  349. if (actual_sort.children[0].getAttribute('data-sort') != default_order){
  350. clicker(choosen_sort);
  351. }
  352. sorted_comments = true;
  353. total_comments = parseInt(finder("li", 'data-role', "post-count", true).innerHTML.match(/([0-9]+) /));
  354. comments = document.getElementById("post-list");
  355. window.clearInterval(initial_timer);
  356. console.log("Finished first timer");
  357. }
  358. }, 100);
  359.  
  360. if (autoload_comments == true){
  361. timer1 = window.setInterval(function (){
  362. if (is_class_name(comments, "post-list loading") == false && sorted_comments == true) {
  363. // Autoload all comments
  364. var more_comments = finder("a", "data-action", "more-posts", true);
  365. if (is_class_name(more_comments, "btn busy") == false && more_comments.parentNode.style.display != "none"){
  366. clicker(more_comments);
  367. }
  368. else if ((finder("li", "class", "post", false).length + finder("li", "class", "post minimized", false).length) == total_comments) {
  369. all_comments_retrieved = true;
  370. window.clearInterval(timer1);
  371. console.log("Finished second timer");
  372. }
  373. }
  374. }, 100);
  375. }
  376. else { all_comments_retrieved = true; } //Necessary to run the next timer
  377.  
  378. // This timer is held in execution because normally the "long" comments often contain many
  379. // images and/or videos, that when the user read this, then Disqus load comments with this
  380. // kind of content and the system at that moment decides whether shorten (or not) the comment
  381. // based on the resultant height (in pixels) of the div where is the comment.
  382. if (expand_comments == true){
  383. timer2 = window.setInterval(function (){// Unhide long comments
  384. var comments = finder("div", "class", "post-body-inner", false);
  385. for (var i=0; i < comments.length; i++) {
  386. var nodes = comments[i].childNodes;
  387. for (var j=0; j < nodes.length; j++) {
  388. if (is_class_name(nodes[j], "post-message-container") == true) {
  389. nodes[j].style.height = "";
  390. }
  391. else if (is_class_name(nodes[j], /message-shadow|more-button|see-more/gi) == true) {
  392. comments[i].removeChild(nodes[j]);
  393. }
  394. }
  395. }
  396. // Adjust the height of the iframe to match with the unshorted comments
  397. // height with media content that are loaded when the user checks them
  398. // (for some reason does not work or only works sometimes, if anyone
  399. // knows how to fix this, then notify on the forum)
  400. if (top.location != self.location) {
  401. var dsq_iframe = top.document.getElementById("dsq-2");
  402. if (dsq_iframe != null){
  403. dsq_iframe.style.height = document.getElementsByTagName("body")[0].clientHeight + "px";
  404. }
  405. }
  406. //console.log("Finished third timer");
  407. }, 500);
  408. }
  409.  
  410. // Check the option "I prefer commenting as guest" when you click in the textbox to write the nick
  411. if (guest_comment == true){
  412. timer3 = window.setInterval(function (){
  413. var nick_textbox = finder("input", "name", "author-name", true);
  414. if (nick_textbox != null){
  415. create_click_event(nick_textbox, function(){
  416. finder("input", "name", "author-guest", true).checked = true;
  417. del_element(finder("input", "name", "author-password", true));
  418. });
  419. window.clearInterval(timer3);
  420. }
  421. }, 100);
  422. }