Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. // ==UserScript==
  2. // @name DuoMoreLingots
  3. // @namespace https://github.com/liuch/duolingo-scripts
  4. // @include https://www.duolingo.com/*
  5. // @version 0.1.7
  6. // @grant none
  7. // @description This script allows you to give more than one lingot in two clicks.
  8. // @description:ru Этот скрипт позволяет давать больше одного лингота за раз.
  9. // @updateURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_morelingots.meta.js
  10. // @downloadURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_morelingots.user.js
  11. // @author FieryCat aka liuch
  12. // @license MIT License
  13. // ==/UserScript==
  14.  
  15. function inject(f) { //Inject the script into the document
  16. var script;
  17. script = document.createElement('script');
  18. script.type = 'text/javascript';
  19. script.setAttribute('name', 'duogivelingots');
  20. script.textContent = '(' + f.toString() + ')(jQuery)';
  21. document.head.appendChild(script);
  22. }
  23. inject(f);
  24.  
  25.  
  26. function f($) {
  27.  
  28. var cache = { id: 0, el: null, top_el: null };
  29.  
  30. function find_love_el(id, root, path) {
  31. if (root)
  32. return $(".discussion-main " + path).eq(-1);
  33. return $("#body-" + id).siblings(".footer").find(path);;
  34. }
  35.  
  36. var update_comment = function(id, love, root) {
  37. var el = cache.el;
  38. if (id != cache.id || !el) {
  39. el = find_love_el(id, root, ".love-number");
  40. if (!el.length) {
  41. el = find_love_el(id, root, ".give-love");
  42. if (!el.length)
  43. return;
  44. if (root)
  45. el.after($('<span><span class="icon icon-lingot-small" /><span class="love-number">0</span></span>'));
  46. else
  47. el.after($('<span class="icon icon-lingot-micro" /><span class="love-number">0</span>'));
  48. el = find_love_el(id, root, ".love-number");
  49. }
  50. cache.id = id;
  51. cache.el = el;
  52. }
  53. el.text(love);
  54. if (!cache.top_el)
  55. cache.top_el = $("#topbar").find("#num_lingots");
  56. if (duo.user.attributes.rupees > 0) {
  57. --duo.user.attributes.rupees;
  58. cache.top_el.text(" " + duo.user.attributes.rupees);
  59. }
  60. };
  61.  
  62. var send_one = function(id, root) {
  63. if (duo.user.attributes.rupees > 0)
  64. $.post("/comments/" + id + "/love", function(d) {"love" in d && update_comment(id, d.love, root);});
  65. };
  66.  
  67. function set_interval_limited(id, root, n, t) {
  68. if(n <= 0) {
  69. cache.el = null;
  70. return;
  71. }
  72. setTimeout(function() {send_one(id, root); set_interval_limited(id, root, n-1, t);}, t);
  73. }
  74.  
  75. var lover = function(id, root) {
  76. var love = parseInt(prompt("How many lingots would you like to give away?", "1"));
  77. if (love > 0 && (love <= 10 || confirm("Are you sure want to give " + love + " lingots away?")))
  78. set_interval_limited(id, root, love, 200);
  79. return false;
  80. };
  81.  
  82. var new_give_lingots = function() {
  83. var el = this.parentElement;
  84. if (el) {
  85. var id = null;
  86. var root = false;
  87. if (el.tagName == "SPAN") {
  88. id = document.location.pathname.match(/^\/comment\/([0-9]+)($|\$)/)[1];
  89. root = true;
  90. } else if (el.tagName == "DIV") {
  91. var i = 4;
  92. while (el) {
  93. if (!--i) {
  94. if (el.id)
  95. id = el.id.match(/^comment-([0-9]+)/)[1];
  96. break;
  97. }
  98. el = el.parentElement;
  99. }
  100. }
  101. if (id)
  102. lover(id, root);
  103. }
  104. return false;
  105. };
  106.  
  107. var main_reg = null;
  108.  
  109. function ajax_complete(e, r, o) {
  110. if (!duo || !duo.user)
  111. return;
  112. if (o.url == "/diagnostics/js_error")
  113. return;
  114.  
  115. if (!main_reg)
  116. main_reg = new RegExp("^(https://forum.duolingo.com)?/comments/[0-9]+($|\\?|/reply|/upvote|/downvote|/love)");
  117. var a = main_reg.exec(o.url);
  118. if (a) {
  119. $("#app").undelegate(".give-love", "click");
  120. $("#app").delegate(".give-love", "click", new_give_lingots);
  121. $(".discussion-comments-list-item").undelegate(".give-love", "click");
  122. $(".discussion-comments-list-item").delegate(".give-love", "click", new_give_lingots);
  123. }
  124. }
  125.  
  126. $(document).ajaxComplete(function(e, r, o) {
  127. ajax_complete(e, r, o);
  128. });
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement