WS一下

选中文本时弹出悬浮框,一键查询sangfor、微步情报

  1. // ==UserScript==
  2. // @name WS一下
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @grant none
  6. // @version 20240726114022
  7. // @description 选中文本时弹出悬浮框,一键查询sangfor、微步情报
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12. function trimLeftRightSpace(str) {
  13. str = str.replace(/^\s+|\s+$/g, '');
  14. str = str.replace(/^\.+|\.+$/g, '');
  15. return str;
  16. }
  17.  
  18. // 创建悬浮框
  19.  
  20. function createFloatingBox(text, callback) {
  21. var floatingBox = document.createElement('div');
  22. floatingBox.style.position = 'absolute';
  23. floatingBox.style.backgroundColor = 'write';
  24. floatingBox.style.border = '2px dashed black'; // 修改边框为虚线
  25. floatingBox.style.padding = '10px';
  26. floatingBox.style.fontSize = '16px';
  27. floatingBox.style.fontWeight = 'bold';
  28. floatingBox.style.zIndex = '99999';
  29. floatingBox.style.borderRadius="25%";
  30. floatingBox.innerHTML = text;
  31. floatingBox.addEventListener('click', callback);
  32. return floatingBox;
  33. }
  34. function base64Encode(str) {
  35. return btoa(unescape(encodeURIComponent(str)));
  36. }
  37.  
  38.  
  39. // 获取选中的文本并显示悬浮框
  40. function showFloatingBoxWithSelectedText() {
  41. var selectedText = window.getSelection().toString();
  42. selectedText = trimLeftRightSpace(selectedText)
  43. if (selectedText) {
  44. var range = window.getSelection().getRangeAt(0);
  45. var rect = range.getBoundingClientRect();
  46. var floatingBox = createFloatingBox('WS一下', function() {
  47. //判断是ip还是域名
  48. const ipPattern = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
  49. const domainPattern = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
  50. const urlPattern = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
  51. if (ipPattern.test(selectedText)) {
  52. window.open('https://ti.sangfor.com.cn/analysis-platform/ip_report/' + encodeURIComponent(base64Encode(selectedText))+`?lang=ZH-CN`, '_blank');
  53. window.open('https://x.threatbook.com/v5/ip/' + selectedText, '_blank');
  54. } else if (domainPattern.test(selectedText)) {
  55. window.open('https://ti.sangfor.com.cn/analysis-platform/dns_report/' + encodeURIComponent(base64Encode(selectedText))+`?lang=ZH-CN`, '_blank');
  56. window.open('https://x.threatbook.com/v5/domain/' + selectedText, '_blank');
  57. } else if (urlPattern.test(selectedText)) {
  58. window.open('https://ti.sangfor.com.cn/analysis-platform/url_report/' + encodeURIComponent(base64Encode(selectedText))+`?lang=ZH-CN`, '_blank');
  59. } else {
  60. alert("选中值【"+selectedText+"】无法查询,请联系开发者进行兼容")
  61. return false
  62. }
  63.  
  64. });
  65. floatingBox.style.left = rect.left + 'px';
  66. floatingBox.style.top = (rect.bottom + window.scrollY) + 'px';
  67. document.body.appendChild(floatingBox);
  68. setTimeout(function() {
  69. document.body.removeChild(floatingBox);
  70. }, 3000); // 悬浮框显示3秒后自动消失
  71. }
  72. }
  73.  
  74. // 监听选中文本事件
  75. document.addEventListener('mouseup', function() {
  76. showFloatingBoxWithSelectedText();
  77. });
  78. })();