旋转水印(网址定制)

在左上角显示旋转 45° 的文字水印,可定制梯形背景、透明度、颜色、大小等

  1. // ==UserScript==
  2. // @name 旋转水印(网址定制)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.8
  5. // @description 在左上角显示旋转 45° 的文字水印,可定制梯形背景、透明度、颜色、大小等
  6. // @author aotmd
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // ✅ 网址配置(不同网站不同样式)
  16. const config = {
  17. "example.com": {
  18. text: "示例网站",
  19. fontSize: 24,
  20. textColor: "#000000",
  21. backgroundColor: "#ffcccc",
  22. opacity: 0.6,
  23. trapezoidHeight: 30, // 梯形高度
  24. strokeColor: "#ff0000"
  25. },
  26. "10.80.20.111": {
  27. text: "测试环境",
  28. fontSize: 30,
  29. textColor: "#ffffff",
  30. backgroundColor: "blue",
  31. opacity: 0.7,
  32. trapezoidHeight: 35,
  33. strokeColor: "#ffffff"
  34. },
  35. "10.97.192.86": {
  36. text: "开发环境",
  37. fontSize: 30,
  38. textColor: "#ffffff",
  39. backgroundColor: "#ff0000",
  40. opacity: 0.7,
  41. trapezoidHeight: 35,
  42. strokeColor: "#ffffff"
  43. }
  44. };
  45.  
  46. // 获取当前网站的 host
  47. const host = window.location.hostname;
  48. let siteConfig = null;
  49. for (const site in config) {
  50. if (host.includes(site)) {
  51. siteConfig = config[site];
  52. break;
  53. }
  54. }
  55.  
  56. // 如果当前网址有匹配的配置,则生成水印
  57. if (siteConfig) {
  58. const { text, fontSize, textColor, backgroundColor, opacity, trapezoidHeight, strokeColor } = siteConfig;
  59.  
  60. const canvas = document.createElement("canvas");
  61. const ctx = canvas.getContext("2d");
  62.  
  63. // 计算文字尺寸
  64. ctx.font = `${fontSize}px Arial`;
  65. const textWidth = fontSize * 3.11;
  66. const textHeight = fontSize * 1.5; // 文字高度
  67.  
  68. // 计算旋转后画布大小
  69. const canvasSize = Math.ceil(Math.sqrt((textWidth + trapezoidHeight * 2) ** 2 + (textHeight + trapezoidHeight * 2) ** 2));
  70. canvas.width = canvasSize;
  71. canvas.height = canvasSize;
  72.  
  73. // 旋转画布
  74. ctx.translate(canvasSize / 2, canvasSize / 2);
  75. ctx.rotate(-Math.PI / 4); // 逆时针旋转 45°
  76. ctx.translate(-canvasSize / 2, -canvasSize / 2);
  77.  
  78. // ✅ 绘制“底边 45° 的梯形”背景
  79. ctx.globalAlpha = opacity;
  80. ctx.fillStyle = backgroundColor;
  81. ctx.beginPath();
  82.  
  83. // 根据梯形高度计算底边的基线长度
  84. const diagonalLength = Math.sqrt(Math.pow(canvasSize, 2) * 2); // 画布的对角线
  85. const bottomWidth = diagonalLength; // 底边等于对角线
  86.  
  87. // 设置梯形的高度(垂直方向增加)
  88. const topWidth = textWidth * 999; // 上边较短
  89. const centerX = canvasSize / 2;
  90. const bottomY = fontSize+trapezoidHeight; // 梯形底边的基线在画布的底部基线位置
  91. const topY = bottomY - fontSize-trapezoidHeight; // 高度垂直向上增加
  92. debugger
  93.  
  94. // 计算梯形的四个顶点
  95. ctx.moveTo(centerX - topWidth / 2, topY); // 顶边左侧
  96. ctx.lineTo(centerX + topWidth / 2, topY); // 顶边右侧
  97. ctx.lineTo(centerX + bottomWidth / 2, bottomY); // 底边右侧
  98. ctx.lineTo(centerX - bottomWidth / 2, bottomY); // 底边左侧
  99. ctx.closePath();
  100. ctx.fill();
  101.  
  102. // ✅ 绘制边框
  103. ctx.strokeStyle = strokeColor;
  104. ctx.lineWidth = 2;
  105. ctx.stroke();
  106.  
  107. // ✅ 绘制文字
  108. ctx.fillStyle = textColor;
  109. ctx.globalAlpha = 1; // 文字不透明
  110. ctx.textAlign = "center"; // 水平居中
  111. ctx.textBaseline = "middle"; // 垂直居中
  112. ctx.font = `${fontSize}px Arial`; // 设置文字大小
  113. ctx.fillText(text, centerX, (topY + bottomY) / 2); // 文字垂直居中
  114.  
  115. // ✅ 生成图片并插入页面
  116. const img = document.createElement("img");
  117. img.src = canvas.toDataURL();
  118. img.style.position = "fixed";
  119. img.style.top = "0px";
  120. img.style.left = "0px";
  121. img.style.zIndex = "9999";
  122. img.style.pointerEvents = "none"; // 让鼠标事件穿透
  123. document.body.appendChild(img);
  124. }
  125. })();

QingJ © 2025

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