PUP Survey Helper

Adds a button to help fill out PUP faculty evaluation surveys

目前为 2025-01-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name PUP Survey Helper
  3. // @namespace Violentmonkey Scripts
  4. // @match https://survey.pup.edu.ph/apps/ofes/survey/*
  5. // @grant none
  6. // @version 1.2
  7. // @author intMeinVoid
  8. // @icon https://www.pup.edu.ph/about/images/PUPLogo.png
  9. // @description Adds a button to help fill out PUP faculty evaluation surveys
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Create and style the floating button
  17. const button = document.createElement('button');
  18. button.textContent = '📝 Set Eval';
  19. button.style.cssText = `
  20. position: fixed;
  21. bottom: 20px;
  22. right: 20px;
  23. padding: 10px 20px;
  24. background-color: #900000;
  25. color: white;
  26. border: none;
  27. border-radius: 5px;
  28. cursor: pointer;
  29. font-size: 14px;
  30. z-index: 9999;
  31. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  32. transition: all 0.3s ease;
  33. `;
  34.  
  35. // Add hover effect
  36. button.addEventListener('mouseenter', () => {
  37. button.style.backgroundColor = '#b00000';
  38. });
  39. button.addEventListener('mouseleave', () => {
  40. button.style.backgroundColor = '#900000';
  41. });
  42.  
  43. // Add the evaluation function
  44. const setEvaluation = async (targetAvg) => {
  45. try {
  46. // If no target average provided, prompt user
  47. if (targetAvg === undefined) {
  48. const input = prompt('Enter desired average (1-5):', '2.5');
  49. if (input === null) return; // User clicked cancel
  50. targetAvg = parseFloat(input);
  51. }
  52.  
  53. // Validate input
  54. if (isNaN(targetAvg)) {
  55. throw new Error('Please enter a valid number');
  56. }
  57.  
  58. if (targetAvg < 1 || targetAvg > 5) {
  59. throw new Error('Target average must be between 1 and 5');
  60. }
  61.  
  62. // Get all radio inputs and calculate total questions
  63. const radios = document.querySelectorAll('input[type="radio"][name^="q"]');
  64. const totalQuestions = radios.length / 5;
  65.  
  66. if (totalQuestions === 0) {
  67. throw new Error('No questions found on the page');
  68. }
  69.  
  70. // Calculate required values
  71. const exactTotal = targetAvg * totalQuestions;
  72. const roundedTotal = Math.round(exactTotal);
  73. const lowerValue = Math.floor(targetAvg);
  74. const higherValue = Math.ceil(targetAvg);
  75. const numberOfHigher = roundedTotal - (lowerValue * totalQuestions);
  76.  
  77. // Validate if average is achievable
  78. if (numberOfHigher < 0 || numberOfHigher > totalQuestions) {
  79. throw new Error(`Target average ${targetAvg} is not achievable with ${totalQuestions} questions`);
  80. }
  81.  
  82. // Clear any previously selected options
  83. radios.forEach(radio => radio.checked = false);
  84.  
  85. // Set scores for each question
  86. for (let i = 1; i <= totalQuestions; i++) {
  87. const score = i <= numberOfHigher ? higherValue : lowerValue;
  88. const questionId = `q${i}${score}`;
  89. const element = document.getElementById(questionId);
  90.  
  91. if (!element) {
  92. throw new Error(`Could not find element with ID: ${questionId}`);
  93. }
  94.  
  95. element.checked = true;
  96. }
  97.  
  98. // Show success message
  99. const msg = `Set ${totalQuestions} questions to average ${targetAvg}\n` +
  100. `(${numberOfHigher} × ${higherValue} and ${totalQuestions - numberOfHigher} × ${lowerValue})`;
  101. alert(msg);
  102.  
  103. } catch (error) {
  104. alert('Error: ' + error.message);
  105. }
  106. };
  107.  
  108. // Add click handler to the button
  109. button.addEventListener('click', () => setEvaluation());
  110.  
  111. // Add the button to the page
  112. document.body.appendChild(button);
  113. })();