timerecorder

记录你在各个网站学习的时间

  1. // ==UserScript==
  2. // @name timerecorder
  3. // @namespace *
  4. // @version 0.4
  5. // @description 记录你在各个网站学习的时间
  6. // @author passer21
  7. // @match *://*/*
  8. // @icon https://img.zcool.cn/community/01271c5d391e43a80120695ccafcc2.jpg@1280w_1l_2o_100sh.jpg
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12. (function () {
  13. 'use strict';
  14. // Your code here...
  15. const setcss = () => {
  16. var newstyles = `
  17. /* CSS */
  18. .button-86 {
  19. all: unset;
  20. width: 80px;
  21. height: 30px;
  22. font-size: 16px;
  23. background: transparent;
  24. border: none;
  25. position: relative;
  26. color: #f0f0f0;
  27. cursor: pointer;
  28. z-index: 1;
  29. padding: 10px 20px;
  30. display: flex;
  31. align-items: center;
  32. justify-content: center;
  33. white-space: nowrap;
  34. user-select: none;
  35. -webkit-user-select: none;
  36. touch-action: manipulation;
  37. }
  38. .button-86::after,
  39. .button-86::before {
  40. content: '';
  41. position: absolute;
  42. bottom: 0;
  43. right: 0;
  44. z-index: -99999;
  45. transition: all .4s;
  46. }
  47.  
  48. .button-86::before {
  49. transform: translate(0%, 0%);
  50. width: 100%;
  51. height: 100%;
  52. background: #28282d;
  53. border-radius: 10px;
  54. }
  55.  
  56. .button-86::after {
  57. transform: translate(10px, 10px);
  58. width: 35px;
  59. height: 35px;
  60. background: #ffffff15;
  61. backdrop-filter: blur(5px);
  62. -webkit-backdrop-filter: blur(5px);
  63. border-radius: 50px;
  64. }
  65.  
  66. .button-86:hover::before {
  67. transform: translate(5%, 20%);
  68. width: 110%;
  69. height: 110%;
  70. }
  71.  
  72. .button-86:hover::after {
  73. border-radius: 10px;
  74. transform: translate(0, 0);
  75. width: 100%;
  76. height: 100%;
  77. }
  78.  
  79. .button-86:active::after {
  80. transition: 0s;
  81. transform: translate(0, 5%);
  82. }`
  83. var styleSheet = document.createElement("style")
  84. styleSheet.innerText = newstyles
  85. document.head.appendChild(styleSheet)
  86. }
  87. var startTime, endTime, timeSpent;
  88. const startcount = () => {
  89. // Get the current time when the page is loaded
  90. startTime = new Date();
  91. //console.log(startTime);
  92. timeSpent = Number(localStorage.getItem("timeSpent")) || 0;
  93. // Get the previous time spent from localStorage or set it to zero if not found
  94. //console.log(timeSpent)
  95. }
  96. const stopcount = () => {
  97. // Get the current time when the page is unloaded
  98. endTime = new Date();
  99. // Calculate the time difference in milliseconds
  100. var timeDiff = (endTime - startTime) / 1000;
  101. // Add the time difference to the previous time spent
  102. timeSpent += timeDiff;
  103. //console.log(timeSpent, endTime, startTime)
  104. // Save the new time spent in localStorage
  105. if (!isNaN(timeSpent)) {
  106. //console.log(timeSpent, 'when storage')
  107. localStorage.setItem("timeSpent", timeSpent);
  108. }
  109. }
  110. const GetTime = () => {
  111. let timer = localStorage.getItem("timeSpent");
  112. //console.log(`total time is ${timer / 60} min`);
  113. return (timer / 60)
  114. }
  115. window.onload = function () {
  116. startcount();
  117. //startcount();
  118. };
  119. window.onbeforeunload = function () {
  120. stopcount();
  121. };
  122. document.addEventListener("visibilitychange", function () {
  123. // If the document is hidden, stop the timer and save the time spent
  124. if (document.hidden) {
  125. stopcount();
  126. } else {
  127. // If the document is visible, resume the timer and update the start time
  128. startcount();
  129. }
  130. });
  131. const showtime = () => {
  132. var iframe = document.createElement("iframe");
  133. iframe.src = "about:blank";
  134. document.body.appendChild(iframe);
  135. let spendingtime = GetTime()
  136. iframe.contentDocument.write(`<html><head><title>you time spending</title></head><body>你在这个网站的努力时长累计为${spendingtime}分钟</body></html>`);
  137. }
  138. const makebutton = () => {
  139. setcss()
  140. // Create a button element
  141. var button = document.createElement("button");
  142. // Set the button text
  143. button.className = 'button-86'
  144. button.innerHTML = "time";
  145. // Set the button style
  146. button.style.position = "fixed";
  147. button.style.bottom = "0%";
  148. button.style.right = "0";
  149. // button.style.width = "50px";
  150. // button.style.height = "50px";
  151. // button.style.borderRadius = "50%";
  152. // button.style.transform = "translate(0%, 0%)";
  153. // button.style.backgroundColor = "blue";
  154. // button.style.color = "white";
  155. // button.style.border = "none";
  156. // Add an event listener for the button click
  157. button.addEventListener("click", function () {
  158. // Do something when the button is clicked
  159. let spendingtime = GetTime()
  160. alert(`大神!您已经在这个网站上学习了${spendingtime}分钟了`);
  161. });
  162. // Append the button to the document body
  163. document.body.appendChild(button);
  164. }
  165. makebutton()
  166. //comment it if you dont need it.
  167. //showtime()
  168. //uncomment it if you need.
  169. })();