AnkiWeb Quiz

Shows quiz on ankiweb

当前为 2017-08-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 1.1.1
  10. // @grant GM_listValues
  11. // @grant GM_deleteValue
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // ==/UserScript==
  15.  
  16. //not empty val
  17. var std = window.eval("require('study').default;"),
  18. defaultDeck = new Deck("question default", "answer default", 10001, 20002),
  19. defaultDecks =
  20. {
  21. defaultId : new Decks(defaultDeck)
  22. }
  23.  
  24. //const
  25. const inBstring = "<awq>",
  26. inEstring = "</awq>",
  27. inBegAnswer = "<awq_answer>",
  28. inEndAnswer = "</awq_answer>",
  29. textDefault = "You need to use this deck more to get more variations";
  30.  
  31. //arrays
  32. var stringArray = [],
  33. tempStrings = [],
  34. falseAnswers = [],
  35. inB = [],
  36. inE = [],
  37. buttons = [],
  38. tempArr = [];
  39.  
  40. //empty val
  41. var searchFor,
  42. trueAnswer,
  43. trueId,
  44. id,
  45. rubyVal,
  46. deck;
  47.  
  48. //prefs
  49. var amountButtons,
  50. debug,
  51. decks,
  52. lastIdChosen;
  53.  
  54. Main();
  55.  
  56. function Main()
  57. {
  58. CssAdd();
  59. SetSettings();
  60. SetEventsOnDecks(document.URL);
  61. }
  62.  
  63. //Settings
  64. function SetSettings()
  65. {
  66. const settings = $("<li class=nav-item></li>").html("<a id=awq_settings class=nav-link>Settings Ankiweb Quiz " + GM_info.script.version + "</a> \
  67. <div id=awq_settingsPanel class=awq_settingsP>\
  68. <form> \
  69. <br> \
  70. Amount Buttons(4-20):<input type=number name=amountBtn id=awq_amountBtn min=4 max=20 value=4></input><br> \
  71. Debug: <input type=checkbox name=debug id=awq_debug></input>\
  72. </form>\
  73. <button id=hideButton class=awq_style>Hide</button>\
  74. </div>\
  75. ");
  76.  
  77. $(".navbar-nav:first").append(settings);
  78. $("#awq_settings").addClass("awq_settings");
  79. $("#awq_settingsPanel").hide();
  80. SetEventSettings();
  81. LoadSettings();
  82. }
  83.  
  84. function LoadSettings()
  85. {
  86.  
  87. DeleteValues("old");
  88.  
  89. //THIS IS ABOUT DEBUG
  90. if (HasValue("awq_debug", false))
  91. {
  92. debug = GM_getValue("awq_debug");
  93. $("#awq_debug").prop("checked", debug);
  94. }
  95.  
  96. //THIS IS ABOUT DECKS
  97. if (HasValue("awq_decks", JSON.stringify(defaultDecks)))
  98. {
  99. decks = JSON.parse(GM_getValue("awq_decks"));
  100. //console.log(decks);
  101. }
  102.  
  103. //THIS IS ABOUT lastIdChosen
  104. if (HasValue("awq_lastIdChosen", 000))
  105. {
  106. lastIdChosen = GM_getValue("awq_lastIdChosen");
  107. GetDeck(lastIdChosen);
  108. }
  109.  
  110. //THIS IS ABOUT BUTTONS
  111. if (HasValue("awq_amountButtons", 8))
  112. {
  113. amountButtons = GM_getValue("awq_amountButtons");
  114. $("#awq_amountBtn").prop("value", amountButtons);
  115. }
  116.  
  117. //Console log prefs with value
  118. console.log("*prefs:");
  119. console.log("*-----*");
  120. var vals = [];
  121. for (var i = 0; i < GM_listValues().length; i++)
  122. {
  123. vals[i] = GM_listValues()[i];
  124. }
  125. for (var i = 0; i < vals.length; i++)
  126. {
  127. console.log("*" + vals[i] + ":" + GM_getValue(vals[i]));
  128. }
  129. console.log("*-----*");
  130. }
  131.  
  132. //Check if value exists or not. optValue = Optional
  133. function HasValue(nameVal, optValue)
  134. {
  135. var vals = [];
  136. for (var i = 0; i < GM_listValues().length; i++)
  137. {
  138. vals[i] = GM_listValues()[i];
  139. }
  140.  
  141. if (vals.length === 0)
  142. {
  143. if (optValue != undefined)
  144. {
  145. GM_setValue(nameVal, optValue);
  146. return true;
  147. } else
  148. {
  149. return false;
  150. }
  151. }
  152.  
  153. for (var i = 0; i < vals.length; i++)
  154. {
  155. if (vals[i] === nameVal)
  156. {
  157. return true;
  158. }
  159. }
  160.  
  161. if (optValue != undefined)
  162. {
  163. GM_setValue(nameVal, optValue);
  164. return true;
  165. } else
  166. {
  167. return false;
  168. }
  169. }
  170.  
  171. //Delete Values
  172. function DeleteValues(nameVal)
  173. {
  174. var vals = [];
  175. for (var i = 0; i < GM_listValues().length; i++)
  176. {
  177. vals[i] = GM_listValues()[i];
  178. }
  179.  
  180. if (vals.length === 0 || typeof nameVal != "string")
  181. {
  182. return;
  183. }
  184.  
  185. switch (nameVal)
  186. {
  187. case "all":
  188. for (var i = 0; i < vals.length; i++)
  189. {
  190. GM_deleteValue(vals[i]);
  191. }
  192. break;
  193. case "old":
  194. for (var i = 0; i < vals.length; i++)
  195. {
  196. if (vals[i] === "debug" || vals[i] === "debugA" || vals[i] === "awq_amountBtn")
  197. {
  198. GM_deleteValue(vals[i]);
  199. }
  200. }
  201. break;
  202. default:
  203. for (var i = 0; i < vals.length; i++)
  204. {
  205. if (vals[i] === nameVal)
  206. {
  207. GM_deleteValue(nameVal);
  208. }
  209. }
  210. break;
  211. }
  212. }
  213.  
  214. //Construction of Deck
  215. function Deck(question, answer, idTimeOne, idTimeTwo)
  216. {
  217. this.question = [question];
  218. this.answer = [answer];
  219. this.idTimeOne = [idTimeOne];
  220. this.idTimeTwo = [idTimeTwo];
  221. }
  222.  
  223. //Construction of Decks
  224. function Decks(cards)
  225. {
  226. this.cards = cards;
  227. this.updateDeck = false;
  228. this.firstTime = true;
  229. this.customSettings = {};
  230. };
  231.  
  232. function SetEventSettings()
  233. {
  234. $("#awq_settings").click(function ()
  235. {
  236. $("#awq_settingsPanel").toggle(1000);
  237. });
  238.  
  239. $("#hideButton").click(function ()
  240. {
  241. $("#awq_settingsPanel").toggle(1000);
  242. });
  243.  
  244. $("#awq_debug").change(function ()
  245. {
  246. GM_setValue("awq_debug", $(this).prop("checked"));
  247. debug = $(this).prop("checked");
  248. alert("Settings has been changed. Please reload the page.");
  249. });
  250.  
  251. $("#awq_amountBtn").change(function ()
  252. {
  253. GM_setValue("awq_amountButtons", $(this).prop("value"));
  254. amountButtons = $(this).prop("value");
  255. alert("Settings has been changed. Please reload the page.");
  256. });
  257. }
  258.  
  259. function SetEventsOnDecks(url)
  260. {
  261. if (url.match(/http:\/\/ankiweb\.net\/decks/i) || url.match(/https:\/\/ankiweb\.net\/decks/i))
  262. {
  263. $("div.light-bottom-border > div:first-child > button").on("mousedown", function ()
  264. {
  265. lastIdChosen = this.id;
  266. GM_setValue("awq_lastIdChosen", lastIdChosen);
  267. });
  268. } else
  269. {
  270. return;
  271. }
  272. }
  273.  
  274. function SetEventsOnStudy(url)
  275. {
  276. if (url.match(/http:\/\/ankiweb\.net\/study/i) || url.match(/https:\/\/ankiweb\.net\/study/i))
  277. {
  278. $("#leftStudyMenu a:first-child").on("mouseover", function ()
  279. {
  280. try
  281. {
  282. UpdateGMDecks();
  283. console.log("UpdateGM");
  284. } catch (e) { console.log(e);}
  285. });
  286. } else
  287. {
  288. return;
  289. }
  290. }
  291.  
  292. function FindIndexes(searchStr, str, caseSensitive)
  293. {
  294. var searchStrLen = searchStr.length;
  295. if (searchStrLen == 0) {
  296. return [];
  297. }
  298. var startIndex = 0, index, indices = [];
  299. if (!caseSensitive) {
  300. str = str.toLowerCase();
  301. searchStr = searchStr.toLowerCase();
  302. }
  303. while ((index = str.indexOf(searchStr, startIndex)) > -1) {
  304. indices.push(index);
  305. startIndex = index + searchStrLen;
  306. }
  307. return indices;
  308. }
  309.  
  310. //css styles adds
  311. function CssAdd()
  312. {
  313. $("head").append($("<!--Start of AnkiWeb Quiz v" + GM_info.script.version + " CSS-->"));
  314.  
  315. $("head").append($("<style type=text/css></style>").text("button.awq_btn { \
  316. \
  317. }"));
  318.  
  319. $("head").append($("<style type=text/css></style>").text("a.awq_settings { \
  320. cursor: pointer;\
  321. }"));
  322.  
  323. $("head").append($("<style type=text/css></style>").text("div.awq_settingsP { \
  324. position:absolute; width:300px; background-color: #fff; border-color: #eee!important; border-radius: .3rem; border: 2px solid transparent; z-index: 150;\
  325. }"));
  326.  
  327. $("head").append($("<style type=text/css></style>").text("button.awq_style { \
  328. 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;\
  329. }"));
  330.  
  331. $("head").append($("<style type=text/css></style>").text("button.awq_style:hover { \
  332. cursor: pointer; color: #fff; background-color: #025aa5; border-color: #01549b; padding: .75rem 1.5rem; font-size: 1rem; border-radius: .3rem; border: 1px solid transparent;\
  333. }"));
  334.  
  335. $("head").append($("<style type=text/css></style>").text("div.awq_rstyle { \
  336. width:100%; margin-top:30px; z-index: 100;\
  337. }"));
  338.  
  339. $("head").append($("<style type=text/css></style>").text("button.awq_true { \
  340. background-color: #75d802; border-color: #75d802;\
  341. }"));
  342.  
  343. $("head").append($("<style type=text/css></style>").text("button.awq_true:hover { \
  344. background-color: #5aa502; border-color: #5aa502;\
  345. }"));
  346.  
  347. $("head").append($("<style type=text/css></style>").text("button.awq_false { \
  348. background-color: #d80275; border-color: #d80275;\
  349. }"));
  350.  
  351. $("head").append($("<style type=text/css></style>").text("button.awq_first { \
  352. background-color: #000; border-color: #000;\
  353. }"));
  354.  
  355. $("head").append($("<style type=text/css></style>").text("button.awq_false:hover { \
  356. background-color: #a5025a; border-color: #a5025a;\
  357. }"));
  358.  
  359. $("head").append($("<!--End of AnkiWeb Quiz v" + GM_info.script.version + " CSS-->"));
  360. }
  361.  
  362. function GetDeck(idDeck)
  363. {
  364. var keyNames = Object.keys(decks);
  365. for (var i in keyNames)
  366. {
  367. if (idDeck == keyNames[i])
  368. {
  369. deck = decks[idDeck].cards;
  370. return;
  371. }
  372. }
  373.  
  374. if(deck == undefined)
  375. {
  376. decks[idDeck] = new Decks(defaultDeck);
  377. deck = decks[idDeck].cards;
  378. return;
  379. }
  380. }
  381.  
  382. //THIS FUNC FOR UPDATING Greasemonkey value JSON OBJECT
  383. function UpdateGMDecks()
  384. {
  385. try
  386. {
  387. if (deck["answer"].length < amountButtons)
  388. {
  389. decks[lastIdChosen].firstTime = true;
  390. }
  391.  
  392. var gmDecks = JSON.stringify(decks);
  393. GM_setValue("awq_decks", gmDecks);
  394. }
  395. catch (e)
  396. {
  397. console.log(e);
  398. }
  399. }
  400.  
  401. $(document).ready(function () {
  402.  
  403. // Append some text to the element with id someText using the jQuery library.
  404. //$("#studynow").append(" more text...................");
  405.  
  406. $("#studynow").click(function () {
  407. setTimeout(function ()
  408. {
  409. SetUI();
  410. SetEventsOnStudy(document.URL);
  411. if (decks[lastIdChosen].firstTime == true)
  412. {
  413. FirstTimeDeck(std.currentCard, std["deck"].cards);
  414. } else
  415. {
  416. var question = $.trim(StripNewLines(StripTags(std.currentCard[1].replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  417. answer = $.trim(StripNewLines(StripTags(std.currentCard[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  418. idTimeOne = std.currentCard[0],
  419. idTimeTwo = std.currentCard[4];
  420. UpdateDeck(question, answer, idTimeOne, idTimeTwo);
  421. }
  422. //searchFor = SearchQuestion();
  423. if (debug)
  424. {
  425. console.log(std);
  426. console.log("---");
  427. //console.log($.trim(StripNewLines(StripTags(asd[1].replace(/<style>[\s\S]*?<\/style>/ig, '')))));
  428. //console.log($.trim(StripNewLines(StripTags(asd[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))));
  429. console.log("---");
  430. //console.log("FirstTime:" + decks[lastIdChosen].firstTime);
  431. console.log(decks);
  432. console.log($.trim($("#rightStudyMenu").text()).split("+"));
  433. //console.log("searchFor:" + searchFor);
  434. }
  435. //GetTrueAnswer(searchFor);
  436. GetTrueAnswerU(std.currentCard[0], std.currentCard[4]);
  437. if (debug) {
  438. console.log('Study Click');
  439. }
  440. }, 1500);
  441. });
  442.  
  443. function NumberOfButtons()
  444. {
  445. var buttons = "";
  446. for (var i = 0; i < amountButtons; i++)
  447. {
  448. buttons += "<button class=awq_btn></button>";
  449. }
  450. return buttons;
  451. }
  452.  
  453. //THIS FUNC FOR FIRST TIME USING DECK AFteR INSTALL SCRIPT
  454. function FirstTimeDeck(currentCard, nextCards)
  455. {
  456. var questions = [$.trim(StripNewLines(StripTags(currentCard[1].replace(/<style>[\s\S]*?<\/style>/ig, ''))))],
  457. answers = [$.trim(StripNewLines(StripTags(currentCard[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, ''))))],
  458. idTimeOnes = [currentCard[0]],
  459. idTimeTwos = [currentCard[4]];
  460.  
  461. for (var i = 0; i < nextCards.length; i++)
  462. {
  463. questions.push($.trim(StripNewLines(StripTags(nextCards[i][1].replace(/<style>[\s\S]*?<\/style>/ig, '')))));
  464. answers.push($.trim(StripNewLines(StripTags(nextCards[i][2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))));
  465. idTimeOnes.push(nextCards[i][0]);
  466. idTimeTwos.push(nextCards[i][4]);
  467. }
  468.  
  469. for (var i = 0; i < questions.length; i++)
  470. {
  471. UpdateDeck(questions[i], answers[i], idTimeOnes[i], idTimeTwos[i]);
  472. }
  473.  
  474. decks[lastIdChosen].firstTime = false;
  475. }
  476.  
  477. //THIS FUNC FOR UPDATING DECK OBJECT
  478. function UpdateDeck(question, answer, idTimeOne, idTimeTwo)
  479. {
  480. //TODO FORCE UPDATE
  481. if (question.length >= 350)
  482. {
  483. question = "CARD TOO LONG: " + question.slice(0, 350);
  484. }
  485.  
  486. if (answer.length >= 350)
  487. {
  488. answer = "CARD TOO LONG: " + answer.slice(0, 350);
  489. }
  490.  
  491. //CHECK FOR REPEAT
  492. for (var i = 0; i < deck["idTimeOne"].length; i++)
  493. {
  494. if (idTimeOne === deck["idTimeOne"][i] && idTimeTwo === deck["idTimeTwo"][i])
  495. {
  496. return;
  497. }
  498. }
  499.  
  500. //First time deck detected(delete default card)
  501. if (deck["idTimeOne"][0] === 10001 && deck["idTimeTwo"][0] === 20002)
  502. {
  503. deck["question"][0] = question;
  504. deck["answer"][0] = answer;
  505. deck["idTimeOne"][0] = idTimeOne;
  506. deck["idTimeTwo"][0] = idTimeTwo;
  507. } else
  508. {
  509. deck["question"].push(question);
  510. deck["answer"].push(answer);
  511. deck["idTimeOne"].push(idTimeOne);
  512. deck["idTimeTwo"].push(idTimeTwo);
  513. }
  514. }
  515.  
  516. function SetUI()
  517. {
  518. const buttonP = $("<button id=awq_quiz class=btn style=margin-left:4px></button>").text("Quiz");
  519. const button = $("<div class=awq_rstyle></div>").html(NumberOfButtons());
  520.  
  521. $(".pt-1").before("<br>");
  522. $(".pt-1").before(button);
  523.  
  524. $("#leftStudyMenu").after(buttonP);
  525.  
  526. SettingsEvents();
  527.  
  528. $("#awq_quiz").addClass("btn-secondary");
  529. $(".awq_btn").addClass("awq_style");
  530. $(".awq_rstyle").hide();
  531. }
  532.  
  533. function SettingsEvents()
  534. {
  535.  
  536. $("#awq_quiz").click(function () {
  537. $(".awq_rstyle").toggle();
  538. });
  539.  
  540. $("#ansbuta").click(function ()
  541. {
  542. CheckStatus($.trim($("#rightStudyMenu").text()).split("+"));
  543. setTimeout(function ()
  544. {
  545. if (debug)
  546. {
  547. console.log("Button check");
  548. }
  549. $("#ease1").click(function ()
  550. {
  551. OtherEventU();
  552. //OtherEvent();
  553. });
  554. $("#ease2").click(function ()
  555. {
  556. OtherEventU();
  557. //OtherEvent();
  558. });
  559. $("#ease3").click(function ()
  560. {
  561. OtherEventU();
  562. //OtherEvent();
  563. });
  564. $("#ease4").click(function ()
  565. {
  566. OtherEventU();
  567. //OtherEvent();
  568. });
  569. }, 250);
  570. });
  571.  
  572. $(".awq_btn").click(function ()
  573. {
  574. if (debug)
  575. {
  576. if ($(this).attr("title"))
  577. {
  578. console.log("html:" + $(this).attr("title"));
  579. console.log("true:" + trueAnswer);
  580. } else
  581. {
  582. console.log("html:" + $(this).html());
  583. console.log("text:" + $(this).text());
  584. console.log("------------------------");
  585. console.log("true:" + trueAnswer);
  586. console.log("************************");
  587. }
  588. }
  589.  
  590. if ($(this).attr("title"))
  591. {
  592. if (trueAnswer == $(this).attr("title"))
  593. {
  594. $(this).addClass("awq_true");
  595. } else
  596. {
  597. $(this).addClass("awq_false");
  598. }
  599. } else
  600. {
  601. if (trueAnswer == $(this).html() || trueAnswer == $(this).text())
  602. {
  603. $(this).addClass("awq_true");
  604. } else
  605. {
  606. $(this).addClass("awq_false");
  607. }
  608. }
  609. });
  610. }
  611.  
  612. function CheckStatus(statusArr)
  613. {
  614. var one = parseInt(statusArr[0]),
  615. two = parseInt(statusArr[1]),
  616. tree = parseInt(statusArr[2]);
  617. if (debug)
  618. {
  619. console.log(one);
  620. console.log(two);
  621. console.log(tree);
  622. }
  623. if ((one + two + tree) === 0)
  624. {
  625. UpdateGMDecks();
  626. } else
  627. {
  628. return;
  629. }
  630. }
  631.  
  632. function EscapeRegExp(string)
  633. {
  634. return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  635. }
  636.  
  637. function SearchQuestion()
  638. {
  639. if (debug)
  640. {
  641. console.log("span: ");
  642. console.log($("awq_question").has("span"));
  643. }
  644. if ($("awq_question").has("span").length >= 1)
  645. {
  646. var contentText = $("awq_question").contents().filter(function ()
  647. {
  648. return this.nodeType == 3;
  649. });
  650.  
  651. var contentSpan = $("awq_question").contents().filter("span");
  652.  
  653. if (debug)
  654. {
  655. console.log(contentText);
  656. console.log(contentSpan);
  657. }
  658.  
  659. rubyVal = "";
  660. var x = 0;
  661. if (contentText >= contentSpan)
  662. {
  663. for (var i = 0; i < contentText.length; i++)
  664. {
  665. rubyVal += $.trim(contentText[i].nodeValue);
  666. if (x < contentSpan.length)
  667. {
  668. rubyVal += "<ruby><rb>";
  669. rubyVal += $.trim($(contentSpan[x]).contents().filter(function ()
  670. {
  671. return this.nodeType == 3;
  672. })[0].nodeValue) + "</rb><rt>";
  673. rubyVal += $(contentSpan[x]).contents()[0].innerHTML + "</rt></ruby>";
  674. x++;
  675. }
  676. }
  677. } else
  678. {
  679. for (var i = 0; i < contentSpan.length; i++)
  680. {
  681. if (x < contentText.length)
  682. {
  683. rubyVal += $.trim(contentText[x].nodeValue);
  684. x++;
  685. }
  686. rubyVal += "<ruby><rb>";
  687. rubyVal += $.trim($(contentSpan[i]).contents().filter(function ()
  688. {
  689. return this.nodeType == 3;
  690. })[0].nodeValue) + "</rb><rt>";
  691. rubyVal += $(contentSpan[i]).contents()[0].innerHTML + "</rt></ruby>";
  692. }
  693. }
  694. return rubyVal;
  695. } else
  696. {
  697. return $.trim($("awq_question").text());
  698. }
  699. }
  700.  
  701. //Replace wrong <br>'s or other html tags, should work perfectly but it isn't >:( Fixed(probably)
  702. function ReplaceString(str)
  703. {
  704. var trueString = str;
  705.  
  706. while (trueString.search("<br />") !== -1)
  707. {
  708. trueString = str.replace(/<br \/>/g, "<br>");
  709. }
  710.  
  711. return trueString;
  712. }
  713.  
  714. function GetTrueAnswerU(idOne, idTwo)
  715. {
  716. for (var i = 0; i < deck["idTimeOne"].length; i++)
  717. {
  718. if (idOne === deck["idTimeOne"][i] && idTwo === deck["idTimeTwo"][i])
  719. {
  720. trueAnswer = deck["answer"][i];
  721. trueId = i;
  722. GetFalseAnswersU(trueId);
  723. return;
  724. }
  725. }
  726. }
  727.  
  728. function GetFalseAnswersU(trueId)
  729. {
  730. tempArr.length = 0;
  731. if (deck["answer"].length <= amountButtons)
  732. {
  733. var temp = [];
  734. temp = temp.concat(deck["answer"]);
  735. if (debug)
  736. {
  737. console.log(temp);
  738. }
  739. for (var i = 0; i < (amountButtons - (deck["answer"].length - 1)); i++)
  740. {
  741. temp.push(textDefault);
  742. }
  743. if (debug)
  744. {
  745. console.log(temp);
  746. }
  747. }
  748. for (var i = 0; i < (amountButtons - 1); i++)
  749. {
  750. if (deck["answer"].length > amountButtons)
  751. {
  752. id = GetRand(deck["answer"]);
  753. if (id != trueId)
  754. {
  755. if (debug)
  756. {
  757. console.log(deck["answer"][id]);
  758. }
  759. falseAnswers[i] = deck["answer"][id];
  760. if (debug)
  761. {
  762. console.log("***False answer " + i + " : " + falseAnswers[i] + " id: " + id);
  763. //console.log("inBegAnswer: " + str.indexOf(inBegAnswer) + " : " + str.indexOf(inEndAnswer) + " inEndAnswer");
  764. }
  765. } else if(id === 0 || id === trueId)
  766. {
  767. id = GetRand(deck["answer"]);
  768. i--;
  769. }
  770. } else
  771. {
  772. id = GetRand(temp);
  773. if (id != trueId)
  774. {
  775. if (debug)
  776. {
  777. console.log(temp[id]);
  778. }
  779. falseAnswers[i] = temp[id];
  780. if (debug)
  781. {
  782. console.log("***False answer " + i + " : " + falseAnswers[i] + " id: " + id);
  783. //console.log("inBegAnswer: " + str.indexOf(inBegAnswer) + " : " + str.indexOf(inEndAnswer) + " inEndAnswer");
  784. }
  785. } else
  786. {
  787. id = GetRand(temp);
  788. i--;
  789. }
  790. }
  791. }
  792. RamdomButton();
  793. }
  794.  
  795. function OtherEventU()
  796. {
  797. if (debug)
  798. {
  799. console.log("Button click");
  800. console.log("---------------");
  801. //console.log(std.currentCard);
  802. //console.log($("awq").text().length);
  803. }
  804.  
  805. //event on Edit button
  806. SetEventsOnStudy(document.URL);
  807.  
  808. $(".awq_rstyle").hide();
  809. $(".awq_btn").removeClass("awq_first");
  810. if (std.currentCard == undefined)
  811. {
  812. setTimeout(function ()
  813. {
  814. if (std.currentCard == undefined)
  815. {
  816. setTimeout(function ()
  817. {
  818. var question = $.trim(StripNewLines(StripTags(std.currentCard[1].replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  819. answer = $.trim(StripNewLines(StripTags(std.currentCard[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  820. idTimeOne = std.currentCard[0],
  821. idTimeTwo = std.currentCard[4];
  822. UpdateDeck(question, answer, idTimeOne, idTimeTwo);
  823. GetTrueAnswerU(idTimeOne, idTimeTwo);
  824. }, 3000);
  825. } else
  826. {
  827. var question = $.trim(StripNewLines(StripTags(std.currentCard[1].replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  828. answer = $.trim(StripNewLines(StripTags(std.currentCard[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  829. idTimeOne = std.currentCard[0],
  830. idTimeTwo = std.currentCard[4];
  831. UpdateDeck(question, answer, idTimeOne, idTimeTwo);
  832. GetTrueAnswerU(idTimeOne, idTimeTwo);
  833. }
  834. }, 1000);
  835. } else
  836. {
  837. var question = $.trim(StripNewLines(StripTags(std.currentCard[1].replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  838. answer = $.trim(StripNewLines(StripTags(std.currentCard[2].replace(/[\s\S]*?(<hr id=answer>)/ig, '').replace(/<style>[\s\S]*?<\/style>/ig, '')))),
  839. idTimeOne = std.currentCard[0],
  840. idTimeTwo = std.currentCard[4];
  841. UpdateDeck(question, answer, idTimeOne, idTimeTwo);
  842. GetTrueAnswerU(idTimeOne, idTimeTwo);
  843. }
  844. }
  845.  
  846. //
  847. //random functions
  848. function InArray(array, el) {
  849. for (var i = 0; i < array.length; i++)
  850. if (array[i] == el) return true;
  851. return false;
  852. }
  853.  
  854. function GetRand(array) {
  855. var rand = Math.floor(Math.random() * array.length);
  856. if (!InArray(tempArr, rand)) {
  857. tempArr.push(rand);
  858. return rand;
  859. }
  860. return GetRand(array);
  861. }
  862. //end of random functions
  863. //
  864.  
  865. function RamdomButton()
  866. {
  867. var allAnswers = [];
  868. buttons.length = 0;
  869. tempArr.length = 0;
  870. allAnswers[0] = trueAnswer;
  871. for (var i = 1; i <= falseAnswers.length; i++) {
  872. allAnswers[i] = falseAnswers[i - 1];
  873. }
  874. if (debug) {
  875. console.log("False answers :");
  876. console.log(falseAnswers);
  877. console.log("ALL answers :");
  878. console.log(allAnswers);
  879. }
  880. for (var i = 0; i < allAnswers.length; i++) {
  881. buttons[i] = $.trim(allAnswers[GetRand(allAnswers)]);
  882. }
  883. if (debug) {
  884. console.log("Random order :) = " + buttons);
  885. // console.log($(".awq_LeftSide").html());
  886. }
  887. UiButtons();
  888. }
  889.  
  890. function UiButtons()
  891. {
  892. const sel = document.querySelectorAll("button.awq_btn");
  893. if (debug)
  894. {
  895. console.log("*HERE UI BUTTONS :");
  896. }
  897.  
  898. for (var i = 0; i < buttons.length; i++)
  899. {
  900. //Delete arttribute
  901. if ($(sel[i]).attr("title"))
  902. {
  903. $(sel[i]).removeAttr("title");
  904. }
  905.  
  906. if (buttons[i].length <= 40 || buttons[i].includes("</ruby>"))
  907. {
  908. $(sel[i]).html(buttons[i]);
  909. } else
  910. {
  911. $(sel[i]).html(buttons[i].slice(0, 40) + "...");
  912. $(sel[i]).attr("title", buttons[i]);
  913.  
  914. //change color of button with textDefault
  915. if ($(sel[i]).attr("title") == textDefault)
  916. {
  917. $(sel[i]).addClass("awq_first");
  918. }
  919. }
  920.  
  921. if (debug)
  922. {
  923. //console.log($(sel[i]).attr("title"));
  924. console.log(buttons[i] + " Length: " + buttons[i].length);
  925. console.log(buttons[i].includes("</ruby>"));
  926. }
  927. }
  928.  
  929.  
  930. CheckPresedButtons();
  931. }
  932.  
  933. function CheckPresedButtons()
  934. {
  935. $(".awq_btn").removeClass("awq_true");
  936. $(".awq_btn").removeClass("awq_false");
  937. //$(".awq_btn").removeClass("awq_first");
  938. }
  939.  
  940. console.log("AnkiWeb Quiz v" + GM_info.script.version + " Initialized");
  941. });
  942.  
  943. function StripTags(string)
  944. {
  945. return string.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?(\/)?>|<\/\w+>/gi, '');
  946. }
  947.  
  948. function StripNewLines(string)
  949. {
  950. return string.replace(/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/gi, '\n');
  951. }
  952.  
  953. // ------------
  954. // TODO
  955. // ------------
  956.  
  957. /* TODO STARTS
  958. ✓ 0)REWRITE EVERYTHING WITHOUT USING GETRESOURCE AND CHANGING CODE //DONE 1.0.0
  959. 0.1)Make custom settings
  960. 0.2)Make force update deck (Because once you updated card, in gm_value will be old version of card)
  961. ✓ 1)Make it only one element of buttons //DONE 0.0.9
  962. ✓ 1.1)Increase numbers of buttons to 10-12(optional through settings???) //DONE 1.1.0
  963. ✓ 2)Make it limit of length answer and put whole in attribute title //DONE 0.1.0
  964. ✓ 3)Make it settings, almost done in 0.1.0 //DONE 0.2.0
  965. ✓ 3.1)Debug //DONE 0.1.0
  966. 3.2)Add txt file ***RESEARCH NEEDED***
  967. 3.2.1)Choose them
  968. 3.3)Make it always show quiz
  969. ✓ 4)Make it full functionality of Japanese deck, partial done in 0.0.8 //DONE 0.0.9 Happy with that :)
  970. 5)Search question in between tags <awq_question> and </awq_question> not in whole sentence, almost done in 0.1.2
  971. ✓ 6)TODO for loop in finding question NEED TEST IT //DONE 0.1.7 BROKEN //DONE 0.1.9
  972. TODO ENDS */