SolverDuo

It solves practice lessons automatically or manually, to increase xp use DuoSolverGrinder tool (https://duosolver.is-great.net).

当前为 2024-12-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SolverDuo
  3. // @name:es SolverDuo
  4. // @namespace Violentmonkey Scripts
  5. // @match https://*.duolingo.com/*
  6. // @grant GM_getValue
  7. // @grant GM_setValue
  8. // @grant GM_addStyle
  9. // @require https://cdn.tailwindcss.com
  10. // @version 1.0.8
  11. // @author DuoSolverGrinder, has as base DuoPower modified totally.
  12. // @description It solves practice lessons automatically or manually, to increase xp use DuoSolverGrinder tool (https://duosolver.is-great.net).
  13. // @description:es Soluciona lecciones de práctica automáticamente o manualmente, para incrementar xp usa la herramienta DuoSolverGrinder (https://duosolver.is-great.net).
  14. // ==/UserScript==
  15.  
  16. let solveTimerId;
  17. let isAutoMode = GM_getValue('isAutoMode', false);
  18. let isPanelShow = GM_getValue('isPanelShow', true);
  19. let solveSpeedList = {'speedSlow': 2000, 'speedMedium': 1000, 'speedFast': 500, 'speedFastest': 0 }
  20. let solveSpeed = GM_getValue('solveSpeed', 'speedMedium');
  21.  
  22.  
  23. const mainLessonFormClass = "[id='root'] > div > div > div > div > div:first-child._3v4ux";
  24.  
  25. let panelHtml = `
  26. <div id="panelShowBttns" class="flex flex-col gap-y-2 z-[111] fixed xl:bottom-4 right-5 bottom-48 hidden">
  27. <button id="panelShowBttn" class="font-bold text-gray-800 shadow-lg rounded-full px-2 bg-indigo-200 border border-2" >+</button>
  28. <a target="_blank"class="px-1" href="https://duosolver.is-great.net/">
  29. <img class="rounded-md w-7 h-7" src="https://duosolvergrinder.github.io/DuoSolverGrinder/dsg.png" >
  30. </a>
  31. </div>
  32. <div id="panelDuoSolver" class="inline-flex flex-col gap-y-4 justify-end rounded-xl fixed xl:bottom-4 right-5 bottom-28 z-[111] border border-1 p-4 bg-indigo-400 dark:bg-gray-900">
  33. <button id="panelHideBttn" title="hide" class="w-6 self-center rounded-lg bg-gray-600 text-gray-200">-</button>
  34. <button id="startBttn" class="px-4 py-2 rounded-md border border-2 bg-blue-600 text-gray-200 dark:bg-gray-300 dark:text-gray-800">Start</button>
  35. <section class="inline-flex rounded-md shadow-sm" role="group">
  36. <button id="speedSlow" type="button" class="speedSelector px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-s-lg dark:bg-gray-800 dark:border-gray-700 dark:text-white dark:focus:ring-blue-500 dark:focus:text-white">
  37. Slow
  38. </button>
  39. <button id="speedMedium" type="button" class="speedSelector px-4 py-2 text-sm font-medium text-gray-900 bg-white border-t border-b border-gray-200 dark:bg-gray-800 dark:border-gray-700 dark:text-white dark:focus:ring-blue-500 dark:focus:text-white">
  40. Medium
  41. </button>
  42. <button id="speedFast" type="button" class="speedSelector px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 dark:bg-gray-800 dark:border-gray-700 dark:text-white dark:focus:ring-blue-500 dark:focus:text-white">
  43. Fast
  44. </button>
  45. <button id="speedFastest" type="button" class="speedSelector px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-e-lg dark:bg-gray-800 dark:border-gray-700 dark:text-white dark:focus:ring-blue-500 dark:focus:text-white">
  46. Fastest
  47. </button>
  48. </section>
  49. <p class="dark:text-gray-200">To grind higher xp, use next link:</p>
  50. <div class="flex justify-center gap-x-2">
  51. <a target="_blank" href="https://github.com/DuoSolverGrinder/DuoSolverGrinder/">
  52. <svg class="dark:hidden text-red-500 w-8 h-8" fill="none" viewBox="0 0 120 120"
  53. stroke="currentColor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" fill="#24292f"/>
  54. </svg>
  55. <svg class="hidden dark:block w-8 h-8" fill="none" viewBox="0 0 120 120" stroke="currentColor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" fill="#fff"/>
  56. </svg>
  57. </a>
  58. <a target="_blank" href="https://duosolver.is-great.net/">
  59. <img class="rounded-md w-7 h-7" src="https://duosolvergrinder.github.io/DuoSolverGrinder/dsg.png" >
  60. </a>
  61. </div>
  62.  
  63. </div>
  64. `;
  65.  
  66.  
  67. let solvesBttnHtml = `
  68. <button id="solveBttn" style="--web-ui_button-background-color: rgb(var(--color-gold)); --web-ui_button-border-color: rgb(var(--color-gold))" class="_1rcV8 _1VYyp _1ursp _7jW2t _3DbUj _38g3s lg:block hidden">Solve</button>
  69. <button id="solveAllBttn" class="_1rcV8 _1VYyp _1ursp _7jW2t _3DbUj _38g3s _2oGJR">Solve All</button>
  70. `;
  71.  
  72. function insertPanelAndBttns()
  73. {
  74. let panel = document.getElementById('panelDuoSolver');
  75. if(!panel) {
  76. document.body.insertAdjacentHTML('beforeend', panelHtml);
  77. let bttn = document.getElementById('startBttn');
  78. bttn.addEventListener('click', startStopMain );
  79. const showBttn = document.getElementById('panelShowBttn');
  80. const hideBttn = document.getElementById('panelHideBttn');
  81. showBttn.addEventListener('click', toggleShowHidePanel );
  82. hideBttn.addEventListener('click', toggleShowHidePanel );
  83. document.querySelectorAll('.speedSelector').forEach((element)=> element.addEventListener('click', speedSolveChange));
  84. updatePanelDisplay();
  85. updateSpeedBttnsActive();
  86. }
  87. if (window.location.pathname === '/lesson' || window.location.pathname === '/practice') {
  88. addButtons();
  89. panel ? panel.children[1].style.display = 'none' : null;
  90. return;
  91. }
  92. panel ? panel.children[1].style.display = '': null;
  93. }
  94.  
  95. window.onload = (event) => {
  96. GM_addStyle('img { max-width: none}');
  97. }
  98.  
  99. setInterval(insertPanelAndBttns, 1000);
  100.  
  101.  
  102. function speedSolveChange()
  103. {
  104. solveSpeed = this.id;
  105. GM_setValue('solveSpeed', solveSpeed);
  106. updateSpeedBttnsActive();
  107. }
  108.  
  109. function updateSpeedBttnsActive()
  110. {
  111. const speedBttns = document.querySelectorAll('.speedSelector');
  112. speedBttns.forEach((element)=> element.classList.remove('bg-gray-900','text-white','font-bold'));
  113. speedBttns.forEach((element)=> {
  114. if(element.id == solveSpeed) {
  115. element.classList.remove('bg-white', 'text-gray-900', 'font-semibold', 'dark:bg-gray-800');
  116. element.classList.add('bg-gray-900', 'text-white', 'font-bold', 'dark:bg-black');
  117. return;
  118. }
  119. element.classList.add('bg-white', 'text-gray-900', 'font-semibold', 'dark:bg-gray-800');
  120. element.classList.remove('bg-gray-900', 'text-white', 'font-bold', 'dark:bg-black');
  121. });
  122.  
  123. }
  124.  
  125. function updatePanelDisplay(display)
  126. {
  127. const panelShowBttns = document.getElementById('panelShowBttns');
  128. const panel = document.getElementById('panelDuoSolver');
  129. if(isPanelShow) {
  130. panel.classList.remove('collapse');
  131. panelShowBttns.classList.add('hidden');
  132. return;
  133. }
  134. GM_setValue('isPanelShow', false);
  135. panel.classList.add('collapse');
  136. panelShowBttns.classList.remove('hidden');
  137. return;
  138.  
  139. }
  140.  
  141. function toggleShowHidePanel()
  142. {
  143. isPanelShow = !GM_getValue('isPanelShow');
  144. GM_setValue('isPanelShow', isPanelShow )
  145. updatePanelDisplay();
  146. }
  147.  
  148. function setAutoMode(state)
  149. {
  150. isAutoMode = state;
  151. GM_setValue('isAutoMode', state);
  152. }
  153.  
  154. function startStopMain()
  155. {
  156. setAutoMode(!isAutoMode);
  157. updateBttnsCaptions();
  158. if(isAutoMode) {
  159. window.location.assign('/practice');
  160. }
  161.  
  162. }
  163.  
  164.  
  165. function addButtons()
  166. {
  167. const checkBttn = document.querySelectorAll('[data-test="player-next"]')[0];
  168. if(!checkBttn) {
  169. return;
  170. }
  171. let solveAllBttn = document.getElementById("solveAllBttn");
  172. if (solveAllBttn !== null) {
  173. return;
  174. }
  175. checkBttn.parentElement.classList.add('flex', 'gap-x-8');
  176. checkBttn.parentElement.insertAdjacentHTML('beforeend',solvesBttnHtml);
  177.  
  178. const solveBttn = document.getElementById("solveBttn");
  179. solveAllBttn = document.getElementById("solveAllBttn");
  180. solveBttn.addEventListener('click', solveOne);
  181. solveAllBttn.addEventListener('click', solvingAll);
  182.  
  183. updateBttnsCaptions();
  184. resetTimerAutoMode();
  185. }
  186.  
  187.  
  188.  
  189. function updateBttnsCaptions()
  190. {
  191. const solveAllBttn = document.getElementById("solveAllBttn");
  192. const startBttn = document.getElementById("startBttn");
  193. if (isAutoMode) {
  194. solveAllBttn ? solveAllBttn.innerText = "PAUSE ALL" : null;
  195. startBttn ? startBttn.innerText = "Stop" : null;
  196. } else {
  197. solveAllBttn ? solveAllBttn.innerText = "SOLVE ALL" : null;
  198. startBttn ? startBttn.innerText = "Start" : null;
  199. }
  200. }
  201.  
  202.  
  203. function solvingAll()
  204. {
  205. setAutoMode(!isAutoMode);
  206. updateBttnsCaptions();
  207. resetTimerAutoMode();
  208. }
  209.  
  210. function solveOne()
  211. {
  212. const practiceAgain = document.querySelector('[data-test="player-practice-again"]');
  213. if (practiceAgain !== null && isAutoMode) {
  214. practiceAgain.click();
  215. return;
  216. }
  217. if(document.querySelector('[data-test="session-complete-slide"]') && isAutoMode && !practiceAgain && window.innerWidth <= 768 && window.location.pathname === '/practice') {
  218. window.location.assign('/practice');
  219. return;
  220. }
  221.  
  222. let subType = "";
  223. try {
  224. window.sol = findReact(document.querySelectorAll(mainLessonFormClass)[0]).props.currentChallenge;
  225. subType = window.sol.challengeGeneratorIdentifier.specificType;
  226. } catch {
  227. let next = document.querySelector('[data-test="player-next"]');
  228. if (next) {
  229. next.click();
  230. }
  231. resetTimerAutoMode();
  232. return;
  233. }
  234. if (!window.sol) {
  235. resetTimerAutoMode();
  236. return;
  237. }
  238.  
  239. let nextButton = document.querySelector('[data-test="player-next"]');
  240. if (!nextButton) {
  241. resetTimerAutoMode();
  242. return;
  243. }
  244.  
  245.  
  246. switch(window.sol.type) {
  247. case "listenMatch":
  248. case "listenIsolation":
  249. case "listenTap":
  250. const buttonSkip = document.querySelector('button[data-test="player-skip"]');
  251. if (buttonSkip) {
  252. buttonSkip.click();
  253. }
  254. break;
  255. case "translate":
  256. switch(subType)
  257. {
  258. case "reverse_translate":
  259. const elm = document.querySelector('textarea[data-test="challenge-translate-input"]');
  260. if(elm) {
  261. const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
  262. nativeInputValueSetter.call(elm, window.sol.correctSolutions ? window.sol.correctSolutions[0] : window.sol.prompt);
  263. let inputEvent = new Event('input', {
  264. bubbles: true
  265. });
  266. elm.dispatchEvent(inputEvent);
  267. }
  268. break;
  269. case "tap":
  270. case "reverse_tap":
  271. translateTapReverseTapSolve();
  272. break;
  273. default:
  274. null;
  275. }
  276. break;
  277. case "assist":
  278. case "gapFill":
  279. document.querySelectorAll('[data-test="challenge-choice"]')[window.sol.correctIndex]?.click();
  280. break;
  281. case "name":
  282. let textInput = document.querySelector('[data-test="challenge-text-input"]');
  283. if(textInput) {
  284. if(window.sol.correctSolutions) {
  285. let nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
  286. nativeInputValueSetter.call(textInput, window.sol.correctSolutions[0]);
  287. let inputEvent = new Event('input', {
  288. bubbles: true
  289. });
  290. textInput.dispatchEvent(inputEvent);
  291. }
  292. }
  293. break;
  294. case "partialReverseTranslate":
  295. let elm = document.querySelector('[data-test*="challenge-partialReverseTranslate"]')?.querySelector("span[contenteditable]");
  296. if(elm) {
  297. let nativeInputNodeTextSetter = Object.getOwnPropertyDescriptor(Node.prototype, "textContent").set;
  298. nativeInputNodeTextSetter.call(elm, window.sol?.displayTokens?.filter(t => t.isBlank)?.map(t => t.text)?.join()?.replaceAll(',', '') );
  299. let inputEvent = new Event('input', {
  300. bubbles: true
  301. });
  302. elm.dispatchEvent(inputEvent);
  303. }
  304. break;
  305. default:
  306. null;
  307. }
  308.  
  309. nextButton.click();
  310. resetTimerAutoMode();
  311. }
  312.  
  313. function translateTapReverseTapSolve() {
  314. const all_tokens = document.querySelectorAll('[data-test$="challenge-tap-token"]');
  315. const correct_tokens = window.sol.correctTokens;
  316. const clicked_tokens = [];
  317. correct_tokens.forEach(correct_token => {
  318. const matching_elements = Array.from(all_tokens).filter(element => element.textContent.trim() === correct_token.trim());
  319. if (matching_elements.length > 0) {
  320. const match_index = clicked_tokens.filter(token => token.textContent.trim() === correct_token.trim()).length;
  321. if (match_index < matching_elements.length) {
  322. matching_elements[match_index].click();
  323. clicked_tokens.push(matching_elements[match_index]);
  324. } else {
  325. clicked_tokens.push(matching_elements[0]);
  326. }
  327. }
  328. });
  329. }
  330.  
  331. function resetTimerAutoMode()
  332. {
  333. if(isAutoMode) {
  334. clearTimeout(solveTimerId);
  335. solveTimerId = setTimeout(solveOne, solveSpeedList[solveSpeed]);
  336. }
  337. }
  338.  
  339. function findReact(dom) {
  340. let reactProps = Object.keys(dom.parentElement).find((key) => key.startsWith("__reactProps$"));
  341. let child = dom?.parentElement?.[reactProps]?.children;
  342. return child?.props?.children?._owner?.stateNode ?? child?._owner?.stateNode;
  343. }