ExamTopic Print

Print function for ExamTopics

  1. // ==UserScript==
  2. // @name ExamTopic Print
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-09-19
  5. // @description Print function for ExamTopics
  6. // @author You
  7. // @icon https://cdn-icons-png.flaticon.com/512/839/839184.png
  8. // @match https://www.examtopics.com/*
  9. // @license MIT
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. function getVoteSummary(element) {
  14. var voteBars = element.getElementsByClassName("vote-bar");
  15. var summary = {};
  16. var totalVotes = 0;
  17.  
  18. for (var i = 0; i < voteBars.length; i++) {
  19. var voteBar = voteBars[i];
  20. var tooltipText = voteBar.getAttribute("data-original-title");
  21. var votes;
  22.  
  23. if (tooltipText.includes("%")) {
  24. var percentage = parseFloat(tooltipText);
  25. votes = Math.round((percentage / 100) * totalVotes);
  26. } else {
  27. votes = parseInt(tooltipText);
  28. }
  29.  
  30. if (!isNaN(votes)) {
  31. var option = voteBar.textContent.trim();
  32. summary[option] = votes;
  33. totalVotes += votes;
  34. }
  35. }
  36.  
  37. return {
  38. total: totalVotes,
  39. votes: summary,
  40. };
  41. }
  42.  
  43. function extractQuestionNumber(str) {
  44. const match = str.match(/question (\d+)/);
  45. if (match) {
  46. return "question_" + match[1];
  47. } else {
  48. return "something_wrong_in_parse_title";
  49. }
  50. }
  51.  
  52. function createWindow(clonedElement) {
  53. var mywindow = window.open("", "PRINT", "height=1754,width=1240");
  54.  
  55. var commonStyles = `
  56. <style type="text/css">
  57. .discussion-header-container {
  58. font-family: Arial !important;
  59. }
  60. .comments-container .comment-content{
  61. font-family: Arial !important;
  62. }
  63. @media print {
  64. @page {
  65. margin-left: 1.25in;
  66. margin-right: 1.25in;
  67. }
  68. }
  69. </style>`;
  70.  
  71. mywindow.document.write(`
  72. <html>
  73. <head>
  74. <title>${extractQuestionNumber(document.title)}</title>
  75. ${commonStyles}`);
  76.  
  77. Array.prototype.forEach.call(document.styleSheets, function (sheet) {
  78. mywindow.document.write(
  79. `<link rel="stylesheet" href="${sheet.href}" type="text/css" />`
  80. );
  81. });
  82.  
  83. mywindow.document.write(`
  84. </head>
  85. <body>
  86. ${clonedElement.innerHTML}
  87. </body>
  88. </html>`);
  89. return mywindow;
  90. }
  91.  
  92. function printFunction() {
  93. var selectedElement = document.querySelectorAll(".col-12")[1];
  94. var clonedElement = selectedElement.cloneNode(true);
  95.  
  96. // calculate vote
  97. const voteSummary = getVoteSummary(clonedElement);
  98.  
  99. console.log(voteSummary);
  100.  
  101. let voteSummaryDiv = document.createElement("div");
  102.  
  103. // Create HTML content
  104. let voteSummaryHTML = `<b>Total: ${voteSummary.total}</b><ul>`;
  105. for (let voteType in voteSummary.votes) {
  106. voteSummaryHTML += `<li>${voteType}: ${voteSummary.votes[voteType]}</li>`;
  107. }
  108. voteSummaryHTML += "</ul>";
  109.  
  110. // Set the HTML content to the new div
  111. voteSummaryDiv.innerHTML = voteSummaryHTML;
  112.  
  113. // Append the new div to the last element with class 'question-answer'
  114. let questionAnswerElements =
  115. clonedElement.getElementsByClassName("question-answer");
  116. let lastQuestionAnswerElement =
  117. questionAnswerElements[questionAnswerElements.length - 1];
  118. lastQuestionAnswerElement.appendChild(voteSummaryDiv);
  119.  
  120. var listItemElements = clonedElement.querySelectorAll("li");
  121. listItemElements.forEach(function (listItem) {
  122. listItem.className = "multi-choice-item";
  123. });
  124.  
  125. var garbege = [
  126. ".all-questions-link",
  127. ".correct-answer-box",
  128. ".disclaimer-box",
  129. "span.badge.badge-success.most-voted-answer-badge",
  130. ".correct-answer",
  131. ".vote-answer-button",
  132. ".voting-summary",
  133. ];
  134.  
  135. garbege.forEach(function (selector) {
  136. var elements = clonedElement.querySelectorAll(selector);
  137. elements.forEach(function (element) {
  138. element.parentNode.removeChild(element);
  139. });
  140. });
  141.  
  142.  
  143. var mywindow = createWindow(clonedElement);
  144. mywindow.print();
  145. }
  146.  
  147. function createButton() {
  148. var printButton = document.createElement("button");
  149. printButton.innerHTML = "Print";
  150. printButton.style.marginLeft = "10px";
  151. printButton.onclick = printFunction;
  152.  
  153. var correctAnswerBox = document.querySelector(".correct-answer-box br");
  154. correctAnswerBox.parentNode.insertBefore(printButton, correctAnswerBox);
  155. }
  156.  
  157. (function () {
  158. "use strict";
  159. window.addEventListener("load", createButton, false);
  160. })();