Clock2

Show time drag

目前为 2020-06-08 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Clock2
  3. // @description Show time drag
  4. // @author figuccio
  5. // @version 0.2
  6. // @namespace https://greasyfork.org/users/237458
  7. // @include *
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js
  9. // ==/UserScript==
  10.  
  11. if( top.location != location ) return;
  12.  
  13. Number.prototype.pad = function(size) {
  14. if( typeof(size) !== "number" ) {
  15. size = 2;
  16. }
  17. var s = String(this);
  18. while (s.length < size) {
  19. s = "0" + s;
  20. }
  21. return s;
  22. }
  23.  
  24. var tod = document.createElement("div");
  25. tod.id = "todClock";
  26.  
  27. tod.setAttribute(
  28. "style",
  29. "top:0;" +
  30. "color:black;" +
  31. "font-family:droid sans mono;" +
  32. "font-size:16pt;" +
  33. "line-height:20px;" +
  34. "position:fixed;" +
  35. "text-align:center;" +
  36. "z-index:99999999999;" +
  37. " background-color:green;"+
  38. "-moz-user-select:none;"+
  39. "cursor:move;"
  40. );
  41.  
  42. function tick() {
  43. var d = new Date();
  44. var Y = d.getFullYear();
  45. var M = (d.getMonth()+1).pad();
  46. var D = d.getDate().pad();
  47. var h = d.getHours().pad();
  48. var m = d.getMinutes().pad();
  49. var s = d.getSeconds().pad();
  50. tod.innerHTML = h + ":" + m + ":" + s+ " "+D + "/" + M + "/" + Y;
  51. }
  52.  
  53. tick();
  54. setInterval(tick, 500);
  55.  
  56. $('html body').append(tod);
  57. ////////////
  58. //Make the DIV element draggagle:
  59. dragElement(document.getElementById("todClock"));
  60.  
  61. function dragElement(elmnt) {
  62. var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  63. if (document.getElementById(elmnt.id + "header")) {
  64. /* if present, the header is where you move the DIV from:*/
  65. document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  66. } else {
  67. /* otherwise, move the DIV from anywhere inside the DIV:*/
  68. elmnt.onmousedown = dragMouseDown;
  69. }
  70.  
  71. function dragMouseDown(e) {
  72. e = e || window.event;
  73. e.preventDefault();
  74. // get the mouse cursor position at startup:
  75. pos3 = e.clientX;
  76. pos4 = e.clientY;
  77. document.onmouseup = closeDragElement;
  78. // call a function whenever the cursor moves:
  79. document.onmousemove = elementDrag;
  80. }
  81.  
  82. function elementDrag(e) {
  83. e = e || window.event;
  84. e.preventDefault();
  85. // calculate the new cursor position:
  86. pos1 = pos3 - e.clientX;
  87. pos2 = pos4 - e.clientY;
  88. pos3 = e.clientX;
  89. pos4 = e.clientY;
  90. // set the element's new position:
  91. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  92. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  93. }
  94.  
  95. function closeDragElement() {
  96. /* stop moving when mouse button is released:*/
  97. document.onmouseup = null;
  98. document.onmousemove = null;
  99. }
  100. }
  101.