AnkiWeb Quiz

Shows quiz on ankiweb

当前为 2017-04-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AnkiWeb Quiz
  3. // @namespace https://greasyfork.org/users/102866
  4. // @description Shows quiz on ankiweb
  5. // @include https://ankiweb.net/*
  6. // @include http://ankiweb.net/*
  7. // @require https://code.jquery.com/jquery-3.1.1.min.js
  8. // @author TiLied
  9. // @version 0.1.3
  10. // @grant GM_getResourceText
  11. // @grant GM_listValues
  12. // @grant GM_deleteValue
  13. // @grant GM_getValue
  14. // @grant GM_setValue
  15. // @resource ankiDeck Japanese.txt
  16. // @resource ankiDeck1 JLPTN5.txt
  17. // @resource ankiDeck2 TheKodanshaKanjiLearnersCourse.txt
  18. // ==/UserScript==
  19.  
  20. //not empty val
  21. var originAnkiDeck = GM_getResourceText("ankiDeck"),
  22. inBstring = "<awq>",
  23. inEstring = "</awq>",
  24. inBegAnswer = "<awq_answer>",
  25. inEndAnswer = "</awq_answer>";
  26.  
  27. //arrays
  28. var stringArray = [],
  29. tempStrings = [],
  30. falseAnswers = [],
  31. inB = [],
  32. inE = [],
  33. buttons = [],
  34. tempArr = [];
  35.  
  36. //empty val
  37. var searchFor,
  38. trueAnswer,
  39. trueId,
  40. id,
  41. rubyVal;
  42.  
  43. //prefs
  44. var amountButtons,
  45. debug;
  46.  
  47. Main();
  48.  
  49. function Main()
  50. {
  51. inB = findIndexes(inBstring, originAnkiDeck);
  52. inE = findIndexes(inEstring, originAnkiDeck);
  53. console.log(inB);
  54. console.log(inE);
  55.  
  56. for (var i = 0; i < inB.length; i++)
  57. {
  58. tempStrings[i] = originAnkiDeck.slice(inB[i] + 5, inE[i]);
  59. //console.log(tempStrings[i]);
  60. }
  61. console.log(tempStrings);
  62. cssAdd();
  63. //setSettings();
  64. }
  65.  
  66. //Settings
  67. function setSettings()
  68. {
  69. const settings = $("<li class=nav-item></li>").html("<a id=awq_settings class=nav-link>Settings Ankiweb Quiz</a> \
  70. <div id=awq_settingsPanel class=awq_settingsP>\
  71. <form> \
  72. <br> \
  73. Debug: <input type=checkbox name=debug id=awq_debug></input>\
  74. </form>\
  75. <button class=awq_style>Hide</button>\
  76. <button class=awq_style>Yep</button>\
  77. </div>\
  78. ");
  79.  
  80.  
  81. $(".navbar-nav:first").append(settings);
  82. $("#awq_settings").addClass("awq_settings");
  83. $("#awq_settingsPanel").hide();
  84. setEventSettings();
  85. loadSettings();
  86. }
  87.  
  88. function loadSettings()
  89. {
  90. var vals = [];
  91. for (var i = 0; i < GM_listValues().length; i++)
  92. {
  93. vals[i] = GM_listValues()[i];
  94. }
  95.  
  96. if (vals.length === 0)
  97. {
  98. GM_setValue("awq_debug", false);
  99. debug = false;
  100. $("#awq_debug").prop("checked", false);
  101. //console.log("debug: " + debug);
  102. }
  103.  
  104. //console.log(vals);
  105.  
  106. for (var i = 0; i < vals.length; i++)
  107. {
  108. if (vals[i] === "awq_debug")
  109. {
  110. debug = GM_getValue("awq_debug");
  111. if (debug)
  112. {
  113. $("#awq_debug").prop("checked", debug);
  114. } else
  115. {
  116. $("#awq_debug").prop("checked", debug);
  117. }
  118. } else
  119. {
  120. //GM_setValue("awq_debug", false);
  121. //debug = false;
  122. //$("#awq_debug").prop("checked", false);
  123. //console.log("debug: " + debug);
  124. }
  125. }
  126.  
  127. if (debug)
  128. {
  129. for (var i = 0; i < vals.length; i++)
  130. {
  131. console.log(vals[i] + " val:" + GM_getValue(vals[i]));
  132. }
  133. }
  134. }
  135.  
  136. function setEventSettings()
  137. {
  138. $("#awq_settings").click(function ()
  139. {
  140. $("#awq_settingsPanel").toggle();
  141. });
  142.  
  143. $("#awq_debug").change(function ()
  144. {
  145. GM_setValue("awq_debug", $(this).prop("checked"));
  146. debug = $(this).prop("checked");
  147. alert("Settings has been changed. Please reload the page.");
  148. });
  149. }
  150.  
  151. function findIndexes(searchStr, str, caseSensitive)
  152. {
  153. var searchStrLen = searchStr.length;
  154. if (searchStrLen == 0) {
  155. return [];
  156. }
  157. var startIndex = 0, index, indices = [];
  158. if (!caseSensitive) {
  159. str = str.toLowerCase();
  160. searchStr = searchStr.toLowerCase();
  161. }
  162. while ((index = str.indexOf(searchStr, startIndex)) > -1) {
  163. indices.push(index);
  164. startIndex = index + searchStrLen;
  165. }
  166. return indices;
  167. }
  168.  
  169. //css styles adds
  170. function cssAdd()
  171. {
  172. $("head").append($("<style type=text/css></style>").text("button.awq_btn { \
  173. \
  174. }"));
  175.  
  176. $("head").append($("<style type=text/css></style>").text("a.awq_settings { \
  177. cursor: pointer;\
  178. }"));
  179.  
  180. $("head").append($("<style type=text/css></style>").text("div.awq_settingsP { \
  181. position:absolute; width:300px; background-color: #fff; border-color: #eee!important; border-radius: .3rem; border: 2px solid transparent; z-index: 150;\
  182. }"));
  183.  
  184. $("head").append($("<style type=text/css></style>").text("button.awq_style { \
  185. cursor: pointer; color: #fff; background-color: #0275d8; border-color: #0275d8; padding: .75rem 1.5rem; font-size: 1rem; border-radius: .3rem; border: 1px solid transparent; max-width:200px; margin:5px;\
  186. }"));
  187.  
  188. $("head").append($("<style type=text/css></style>").text("button.awq_style:hover { \
  189. cursor: pointer; color: #fff; background-color: #025aa5; border-color: #01549b; padding: .75rem 1.5rem; font-size: 1rem; border-radius: .3rem; border: 1px solid transparent;\
  190. }"));
  191.  
  192. $("head").append($("<style type=text/css></style>").text("div.awq_rstyle { \
  193. width:100%; margin-top:30px; z-index: 100;\
  194. }"));
  195.  
  196. $("head").append($("<style type=text/css></style>").text("button.awq_true { \
  197. background-color: #75d802; border-color: #75d802;\
  198. }"));
  199.  
  200. $("head").append($("<style type=text/css></style>").text("button.awq_true:hover { \
  201. background-color: #5aa502; border-color: #5aa502;\
  202. }"));
  203.  
  204. $("head").append($("<style type=text/css></style>").text("button.awq_false { \
  205. background-color: #d80275; border-color: #d80275;\
  206. }"));
  207.  
  208. $("head").append($("<style type=text/css></style>").text("button.awq_false:hover { \
  209. background-color: #a5025a; border-color: #a5025a;\
  210. }"));
  211. }
  212.  
  213. $(document).ready(function () {
  214.  
  215. // Append some text to the element with id someText using the jQuery library.
  216. //$("#studynow").append(" more text...................");
  217.  
  218. $("#studynow").click(function () {
  219. setTimeout(function ()
  220. {
  221. setUI();
  222. searchFor = searchQuestion();
  223. if (debug)
  224. {
  225. console.log("searchFor:" + searchFor);
  226. }
  227. getTrueAnswer(searchFor);
  228. if (debug) {
  229. console.log('Study Click');
  230. }
  231. }, 1500);
  232. });
  233.  
  234. function setUI()
  235. {
  236. const buttonP = $("<button id=awq_quiz class=btn style=margin-left:4px></button>").text("Quiz");
  237. const button = $("<div class=awq_rstyle></div>").html("<button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button><button class=awq_btn></button>");
  238.  
  239. $(".pt-1").before("<br>");
  240. $(".pt-1").before(button);
  241.  
  242. $("#leftStudyMenu").after(buttonP);
  243.  
  244. settingsEvents();
  245.  
  246. $("#awq_quiz").addClass("btn-secondary");
  247. $(".awq_btn").addClass("awq_style");
  248. $(".awq_rstyle").hide();
  249. }
  250.  
  251. function settingsEvents()
  252. {
  253.  
  254. $("#awq_quiz").click(function () {
  255. $(".awq_rstyle").toggle();
  256. });
  257.  
  258. $("#ansbuta").click(function ()
  259. {
  260. setTimeout(function ()
  261. {
  262. if (debug)
  263. {
  264. console.log("Button check");
  265. }
  266. $("#ease1").click(function ()
  267. {
  268. otherEvent();
  269. });
  270. $("#ease2").click(function ()
  271. {
  272. otherEvent();
  273. });
  274. $("#ease3").click(function ()
  275. {
  276. otherEvent();
  277. });
  278. $("#ease4").click(function ()
  279. {
  280. otherEvent();
  281. });
  282. }, 500);
  283. });
  284.  
  285. $(".awq_btn").click(function ()
  286. {
  287. if (debug)
  288. {
  289. if ($(this).attr("title"))
  290. {
  291. console.log("html:" + $(this).attr("title"));
  292. console.log("true:" + trueAnswer);
  293. } else
  294. {
  295. console.log("html:" + $(this).html);
  296. console.log("true:" + trueAnswer);
  297. }
  298. }
  299.  
  300. if ($(this).attr("title"))
  301. {
  302. if (trueAnswer == $(this).attr("title"))
  303. {
  304. $(this).addClass("awq_true");
  305. } else
  306. {
  307. $(this).addClass("awq_false");
  308. }
  309. } else
  310. {
  311. if (trueAnswer == $(this).html())
  312. {
  313. $(this).addClass("awq_true");
  314. } else
  315. {
  316. $(this).addClass("awq_false");
  317. }
  318. }
  319. });
  320. }
  321.  
  322. function escapeRegExp(string)
  323. {
  324. return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  325. }
  326.  
  327. function searchQuestion()
  328. {
  329. if (debug)
  330. {
  331. console.log("span: ");
  332. console.log($("awq_question").has("span"));
  333. }
  334. if ($("awq_question").has("span").length >= 1)
  335. {
  336. var contentText = $("awq_question").contents().filter(function ()
  337. {
  338. return this.nodeType == 3;
  339. });
  340.  
  341. var contentSpan = $("awq_question").contents().filter(function ()
  342. {
  343. return this.nodeType == 1;
  344. });
  345.  
  346. if (debug)
  347. {
  348. console.log(contentText);
  349. console.log(contentSpan);
  350. }
  351.  
  352. rubyVal = "";
  353.  
  354.  
  355. //This is if first goes hiragana/katakana
  356. if (contentText[0].nodeValue != "")
  357. {
  358. rubyVal = $.trim(contentText[0].nodeValue);
  359. rubyVal += "<ruby><rb>";
  360.  
  361. rubyVal += $.trim($(contentSpan[0]).contents().filter(function ()
  362. {
  363. return this.nodeType == 3;
  364. })[0].nodeValue) + "</rb><rt>";
  365.  
  366. rubyVal += $(contentSpan[0]).contents()[0].innerHTML + "</rt></ruby>";
  367.  
  368. //After kanji goes hiragana/katakana if not return
  369. if (contentText[1] != null)
  370. {
  371. rubyVal += $.trim(contentText[1].nodeValue);
  372. if (contentSpan[1] != null)
  373. {
  374. rubyVal += "<ruby><rb>";
  375. rubyVal += $.trim($(contentSpan[1]).contents().filter(function ()
  376. {
  377. return this.nodeType == 3;
  378. })[0].nodeValue) + "</rb><rt>";
  379.  
  380. rubyVal += $(contentSpan[1]).contents()[0].innerHTML + "</rt></ruby>";
  381.  
  382. //After kanji goes hiragana/katakana if not return
  383. if (contentText[2] != null)
  384. {
  385. rubyVal += $.trim(contentText[2].nodeValue);
  386. if (contentSpan[2] != null)
  387. {
  388. //TODO THIRD
  389. } else
  390. {
  391. if (debug)
  392. {
  393. console.log("Here actua this: " + rubyVal);
  394. }
  395. return rubyVal;
  396. }
  397. } else
  398. {
  399. if (debug)
  400. {
  401. console.log("Here actua this: " + rubyVal);
  402. }
  403. return rubyVal;
  404. }
  405. } else
  406. {
  407. if (debug)
  408. {
  409. console.log("Here actua this: " + rubyVal);
  410. }
  411. return rubyVal;
  412. }
  413. } else
  414. {
  415. if (debug)
  416. {
  417. console.log("Here actua this: " + rubyVal);
  418. }
  419. return rubyVal;
  420. }
  421.  
  422. if (debug)
  423. {
  424. console.log("value first:" + contentText[0].nodeValue);
  425. console.log(contentSpan);
  426. console.log(contentSpan[0].innerHTML);
  427. }
  428. }
  429.  
  430. if (debug)
  431. {
  432. console.log(contentText);
  433. console.log("IT DOES");
  434. console.log($("awq_question").contents());
  435. //rubyVal = $("awq_question").contents().filter(function ()
  436. //{
  437. // return this.nodeType == 3;
  438. //})[0].nodeValue;
  439. console.log("Here actua this: " + rubyVal);
  440. }
  441. } else
  442. {
  443. return $.trim($("awq_question").html());
  444. }
  445. }
  446.  
  447. function getTrueAnswer(sFor)
  448. {
  449. var regex = '(^|\\s|\\b|(n\\>))';
  450. var tempQuestion;
  451. var strQ;
  452. regex += escapeRegExp(sFor);
  453. regex += '($|\\s|\\b|(\\<\\/a))';
  454.  
  455. if (debug)
  456. {
  457. console.log(regex);
  458. }
  459.  
  460. for (var i = 0; i < tempStrings.length; i++) {
  461. //console.log('sFor =' + sFor + " leng " + sFor.length + " debug : " + new RegExp(regex, "g").test(tempStrings[i]));
  462. //contains = tempStrings[i].matches(".*\\bram\\b.*");
  463. //tempQuestion = '';
  464. //strQ = '';
  465. //strQ = tempStrings[i].toString();
  466. //tempQuestion = $.trim(str.slice(str.indexOf("<awq_question>") + 14, str.indexOf("</awq_question>")));
  467. //console.log(tempQuestion);
  468. if (new RegExp(regex, "g").test(tempStrings[i]))
  469. {
  470. const str = tempStrings[i].toString();
  471. trueAnswer = $.trim(str.slice(str.indexOf(inBegAnswer) + 12, str.indexOf(inEndAnswer)));
  472. trueId = i;
  473. if (debug)
  474. {
  475. //console.log(tempStrings[i - 1]);
  476. console.log(str);
  477. console.log(tempQuestion);
  478. //console.log(tempStrings[i + 1]);
  479. console.log("True answer : " + trueAnswer + " id trueAnsw = " + trueId);
  480. }
  481. getFalseAnswers(trueId);
  482. break;
  483. }
  484. }
  485. }
  486.  
  487. function getFalseAnswers(trueId) {
  488. tempArr.length = 0;
  489. for (var i = 0; i < 7; i++) {
  490. id = get_rand(tempStrings);
  491. if (id != trueId) {
  492. if (debug) {
  493. console.log(tempStrings[id]);
  494. }
  495. const str = tempStrings[id].toString();
  496. falseAnswers[i] = str.slice(str.indexOf(inBegAnswer) + 12, str.indexOf(inEndAnswer));
  497. if (debug) {
  498. console.log("***False answer " + i + " : " + falseAnswers[i] + " id: " + id);
  499. //console.log("inBegAnswer: " + str.indexOf(inBegAnswer) + " : " + str.indexOf(inEndAnswer) + " inEndAnswer");
  500. }
  501. } else {
  502. id = get_rand(tempStrings);
  503. i--;
  504. }
  505. }
  506. ramdomButton();
  507. }
  508.  
  509. function otherEvent()
  510. {
  511. if (debug) {
  512. console.log("Button click");
  513. console.log("---------------");
  514. }
  515. searchFor = "";
  516. //searchFor = $("awq_question").html();
  517. searchFor = searchQuestion();
  518. if (debug) {
  519. console.log("searchFor:" + searchFor);
  520. console.log($("awq").text().length);
  521. }
  522. $(".awq_rstyle").hide();
  523. if (searchFor == "") {
  524. setTimeout(function () {
  525. if ($("awq").text().length === 0) {
  526. setTimeout(function () {
  527. //searchFor = $("awq_question").html();
  528. searchFor = searchQuestion();
  529. if (debug) {
  530. console.log("searchFor:::" + searchFor);
  531. }
  532. getTrueAnswer(searchFor);
  533. }, 3000);
  534. } else {
  535. //searchFor = $("awq_question").html();
  536. searchFor = searchQuestion();
  537. if (debug) {
  538. console.log("searchFor::" + searchFor);
  539. }
  540. getTrueAnswer(searchFor);
  541. }
  542. }, 1000);
  543. } else {
  544. getTrueAnswer(searchFor);
  545. }
  546. }
  547.  
  548. //random functions
  549. function in_array(array, el) {
  550. for (var i = 0 ; i < array.length; i++)
  551. if (array[i] == el) return true;
  552. return false;
  553. }
  554.  
  555. function get_rand(array) {
  556. var rand = Math.floor(Math.random() * array.length);
  557. if (!in_array(tempArr, rand)) {
  558. tempArr.push(rand);
  559. return rand;
  560. }
  561. return get_rand(array);
  562. }
  563. //end of random functions
  564.  
  565. function ramdomButton()
  566. {
  567. buttons.length = 0;
  568. tempArr.length = 0;
  569. var allAnswers = [];
  570. allAnswers[0] = trueAnswer;
  571. for (var i = 1; i <= falseAnswers.length; i++) {
  572. allAnswers[i] = falseAnswers[i - 1];
  573. }
  574. if (debug) {
  575. console.log("False answers :");
  576. console.log(falseAnswers);
  577. console.log("ALL answers :");
  578. console.log(allAnswers);
  579. }
  580. for (var i = 0; i < allAnswers.length; i++) {
  581. buttons[i] = $.trim(allAnswers[get_rand(allAnswers)]);
  582. }
  583. if (debug) {
  584. console.log("Random order :) = " + buttons);
  585. // console.log($(".awq_LeftSide").html());
  586. }
  587. uiButtons();
  588. }
  589.  
  590. function uiButtons()
  591. {
  592. const sel = document.querySelectorAll("button.awq_btn");
  593. if (debug)
  594. {
  595. console.log("*HERE UI BUTTONS :");
  596. }
  597.  
  598. for (var i = 0; i < buttons.length; i++)
  599. {
  600. //Delete arttribute
  601. if ($(sel[i]).attr("title"))
  602. {
  603. $(sel[i]).removeAttr("title");
  604. }
  605.  
  606. if (buttons[i].length <= 40 || buttons[i].includes("</ruby>"))
  607. {
  608. $(sel[i]).html(buttons[i]);
  609. } else
  610. {
  611. $(sel[i]).html(buttons[i].slice(0, 40) + "...");
  612. $(sel[i]).attr("title", buttons[i]);
  613. }
  614. if (debug)
  615. {
  616. //console.log(sel[i]);
  617. console.log(buttons[i] + " Length: " + buttons[i].length);
  618. console.log(buttons[i].includes("</ruby>"));
  619. }
  620. }
  621.  
  622. checkPresedButtons();
  623. }
  624.  
  625. function checkPresedButtons()
  626. {
  627. $(".awq_btn").removeClass("awq_true");
  628. $(".awq_btn").removeClass("awq_false");
  629. }
  630.  
  631. console.log("AnkiWeb Quiz v" + GM_info.script.version + " Initialized");
  632. });
  633.  
  634.  
  635. // ------------
  636. // TODO
  637. // ------------
  638.  
  639. /* TODO STARTS
  640. ✓ 1)Make it only one element of buttons //DONE 0.0.9
  641. 1.1)Increase numbers of buttons to 10-12(optional through settings???)
  642. ✓ 2)Make it limit of length answer and put whole in attribute title //DONE 0.1.0
  643. 3)Make it settings, almost done in 0.1.0
  644. ✓ 3.1)Debug //DONE 0.1.0
  645. 3.2)Add txt file ***RESEARCH NEEDED***
  646. 3.2.1)Choose them
  647. 3.3)Make it always show quiz
  648. ✓ 4)Make it full functionality of Japanese deck, partial done in 0.0.8 //DONE 0.0.9 Happy with that :)
  649. 5)Search question in between tags <awq_question> and </awq_question> not in whole sentence, almost done in 0.1.2
  650. 6)TODO for loop in finding question
  651. TODO ENDS */