AnkiWeb Quiz

Shows quiz on ankiweb

目前为 2017-05-30 提交的版本,查看 最新版本

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