fast search

注意: 该脚实现选中文本快速复制,快速搜索的功能

当前为 2022-01-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name fast search
  3. // @namespace https://github.com/peihaojie/Greasemonkey-script
  4. // @description 注意: 该脚实现选中文本快速复制,快速搜索的功能
  5. // @description 注意: 需要增加适配网站,请手动修改 @include
  6. // @include *
  7. // @version 1.0
  8. // @icon https://raw.githubusercontent.com/peihaojie/Greasemonkey-script/master/icon.png
  9. // ==/UserScript==
  10.  
  11. class InitSearch {
  12. #href =
  13. "https://at.alicdn.com/t/font_3148281_rakewyhjzvo.css?spm=a313x.7781069.1998910419.44&file=font_3148281_rakewyhjzvo.css";
  14.  
  15. #style = `
  16. *::selection {
  17. background: #CCCCCC;
  18. color: #3399CC;
  19. }
  20.  
  21. .search--btn__wrap {
  22. width: 180px;
  23. height: 30px;
  24. border-radius: 5px;
  25. background: #fff;
  26. position: fixed;
  27. cursor: pointer;
  28. box-shadow: 0px 0px 5px 2px #9e9e9e;
  29. overflow: hidden;
  30. display: flex;
  31. }
  32.  
  33. .search--btn__wrap .search--btn {
  34. cursor: pointer;
  35. width: 30px;
  36. height: 30px;
  37. font-size: 20px;
  38. display: inline-flex;
  39. align-items: center;
  40. justify-content: center;
  41. }
  42.  
  43. .search--btn__wrap .search--btn:hover {
  44. background-color: #9e9e9e;
  45. }
  46. `;
  47. constructor() {
  48. this.initStyle();
  49. this.mouseupListener();
  50. }
  51.  
  52. initStyle() {
  53. const stylesheet = document.createElement("link");
  54. stylesheet.rel = "stylesheet";
  55. stylesheet.href = this.#href;
  56. document.head.appendChild(stylesheet);
  57. const style = document.createElement("style");
  58. const styleText = document.createTextNode(this.#style);
  59. style.appendChild(styleText);
  60. document.head.appendChild(style);
  61. }
  62.  
  63. mouseupListener() {
  64. document.body.addEventListener(
  65. "mouseup",
  66. (e) => {
  67. const selection = document.getSelection();
  68. const selectStr = selection.toString();
  69.  
  70. const delBtn = document.getElementById("btn");
  71. delBtn && document.body.removeChild(delBtn);
  72.  
  73. if (selectStr === "") {
  74. return false;
  75. }
  76.  
  77. const { anchorOffset, focusOffset, isCollapsed } = selection;
  78.  
  79. if (isCollapsed) {
  80. return false;
  81. }
  82.  
  83. const position = this.getPosition(e, anchorOffset, focusOffset);
  84. const btnWrap = this.createBtnWrap(position, selectStr);
  85. document.body.appendChild(btnWrap);
  86. },
  87. false
  88. );
  89. }
  90.  
  91. getPosition(e, anchorOffset, focusOffset) {
  92. let left = e.clientX;
  93. let top = e.clientY;
  94. if (anchorOffset > focusOffset) {
  95. left -= 180;
  96. top -= 30;
  97. }
  98. return { left: `${left}px`, top: `${top}px` };
  99. }
  100.  
  101. createBtnWrap({ left, top }, selectStr) {
  102. const btn = document.createElement("div");
  103. btn.classList.add("search--btn__wrap");
  104. btn.style.left = left;
  105. btn.style.top = top;
  106. btn.setAttribute("id", "btn");
  107. btn.setAttribute("data-text", selectStr);
  108. this.createBtnList(btn);
  109. return btn;
  110. }
  111.  
  112. createBtnList(btn) {
  113. const btnList = [
  114. {
  115. url: "https://www.google.com/search?q=",
  116. title: "google搜索",
  117. icon: "icon-google",
  118. },
  119. {
  120. url: "https://cn.bing.com/search?q=",
  121. title: "必应搜索",
  122. icon: "icon-Bing",
  123. },
  124. {
  125. url: "https://www.baidu.com/s?wd=",
  126. title: "百度一下",
  127. icon: "icon-baidu",
  128. },
  129. {
  130. url: "https://www.so.com/s?q=",
  131. title: "360搜索",
  132. icon: "icon-360logo",
  133. },
  134. {
  135. url: "https://www.sogou.com/web?query=",
  136. title: "搜狗搜索",
  137. icon: "icon-sougoushuru",
  138. },
  139. {
  140. url: false,
  141. title: "点击复制",
  142. icon: "icon-fuzhi",
  143. },
  144. ];
  145. btnList.forEach((btnItem) => this.createBtnItem(btn, btnItem));
  146. }
  147.  
  148. createBtnItem(btn, { url, title, icon }) {
  149. const search = document.createElement("div");
  150. search.classList.add("search--btn", "iconfont", icon);
  151. search.setAttribute("title", title);
  152. search.addEventListener("mouseup", (e) => {
  153. const text = btn.getAttribute("data-text");
  154. switch (url) {
  155. case false:
  156. this.copyText(text);
  157. break;
  158. default:
  159. window.open(`${url}${text}`);
  160. break;
  161. }
  162. e.stopPropagation();
  163. document.body.removeChild(btn);
  164. });
  165. btn.appendChild(search);
  166. }
  167.  
  168. copyText(text) {
  169. const input = document.createElement("input");
  170. input.value = text;
  171. document.body.appendChild(input);
  172. input.select();
  173. document.execCommand("copy");
  174. document.body.removeChild(input);
  175. }
  176. }
  177.  
  178. window.onload = () => {
  179. new InitSearch();
  180. };

QingJ © 2025

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