AnkiWeb Quiz

Shows quiz on ankiweb

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

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