MessageAlerter

Use messager instead of native alert

  1. // ==UserScript==
  2. // @name MessageAlerter
  3. // @namespace https://blog.zheeeng.me
  4. // @version 1.0
  5. // @description Use messager instead of native alert
  6. // @author hi@zheeeng.me
  7. // @match http*://*/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. ;(function() {
  13. 'use strict';
  14.  
  15. function notify (message) {
  16. if (!message) return null
  17. var div = document.createElement('div')
  18. div.style = 'position:fixed;top:16px;width:100%;mrgin-top:-100%;transition:margin-top .5s ease;awidth:100%;z-index:99999;text-align:center;font-size:14px;line-height:1.5;'
  19. var div2 = document.createElement('div')
  20. div2.style = 'display:inline-block;padding:8px 16px;background-color:white;border-radius:4px;box-shadow:0 4px 12px rgba(0,0,0,0.15);'
  21. div.appendChild(div2)
  22. var icon = document.createElement('i')
  23. icon.style = 'display:inline-block;margin-right:8px;font-size:16px;font-style:normal;color:white;background-color:red;width:24px;height:24px;border-radius:50%;'
  24. icon.appendChild(document.createTextNode('x'))
  25. var textNode = document.createTextNode(message)
  26. div2.appendChild(icon)
  27. div2.appendChild(textNode)
  28.  
  29. document.body.appendChild(div)
  30.  
  31. function position (marginTop) {
  32. div.style.marginTop = marginTop
  33. }
  34.  
  35. function dismiss () {
  36. document.body.removeChild(div)
  37. }
  38.  
  39. return {
  40. position: position,
  41. dismiss: dismiss
  42. }
  43. }
  44.  
  45. var positionFns = []
  46.  
  47. window.alert= function (message) {
  48. var ops = notify(message)
  49.  
  50. if (ops) {
  51. positionFns.push(ops.position)
  52. setTimeout(function () {
  53. ops.position((positionFns.length - 1) * 50 + 'px')
  54. }, 60)
  55. setTimeout(function () {
  56. ops.dismiss()
  57. positionFns = positionFns.filter(function (positionFn) { return positionFn !== ops.position })
  58. positionFns.forEach(function (positionFn, idx) { positionFn(idx * 50 + 'px') })
  59. }, 3000)
  60. }
  61. }
  62. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址