FKCSDN

try to take over the world!

目前為 2020-09-11 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name FKCSDN
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description try to take over the world!
  6. // @author Bryce
  7. // @include *://www.baidu.com/*
  8. // @include *://www.dogedoge.com/*
  9. // @include *://www.so.com/*
  10. // @include *://www.sogou.com/*
  11. // @include *://www.google.com/*
  12. // @include *://duckduckgo.com/*
  13. // @include *.bing.com/*
  14.  
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18.  
  19. (function() {
  20. 'use strict';
  21. var move = false
  22. var insertHtml = `
  23. <div style="" class="fixed-btn">
  24. <div class="fk-box">
  25. <div class="fk-csdn">FK CSDN</div>
  26. <label class="fk-item">
  27. <input type="checkbox" name="fk-csdn" checked="true">
  28. <div class="slider round"></div>
  29. </label>
  30. </div>
  31. </div>`
  32.  
  33. var insertStyle = `<style type="text/css">
  34. .fixed-btn {
  35. -webkit-user-select: none;
  36. position: fixed;
  37. top: 50px;
  38. right: 50px;
  39. border-radius: 50px;
  40. width: 100px;
  41. height: 100px;
  42. background-color: #f2f8ff;
  43. z-index: 10000;
  44. }
  45.  
  46. .slider.round {
  47. border-radius: 34px;
  48. }
  49.  
  50. .slider {
  51. position: absolute;
  52. cursor: pointer;
  53. width: 40px;
  54. height: 20px;
  55. top: 0;
  56. left: 0;
  57. right: 0;
  58. bottom: 0;
  59. background-color: #ccc;
  60. -webkit-transition: .4s;
  61. transition: .4s;
  62. }
  63.  
  64. .slider.round:before {
  65. border-radius: 50%;
  66. }
  67.  
  68. .slider:before {
  69. position: absolute;
  70. content: "";
  71. height: 16px;
  72. width: 16px;
  73. left: 2px;
  74. bottom: 2px;
  75. background-color: white;
  76. -webkit-transition: .4s;
  77. transition: .4s;
  78. }
  79.  
  80. .fk-item input {
  81. display: none;
  82. }
  83.  
  84. .fk-item {
  85. position: absolute;
  86. right: 0;
  87. top: 10px;
  88. display: inline-block;
  89. vertical-align: middle;
  90. width: 40px;
  91. height: 20px;
  92. top: 6px;
  93. }
  94.  
  95. input:checked+.slider {
  96. background-color: #2196F3;
  97. }
  98.  
  99. input:checked+.slider:before {
  100. -webkit-transform: translateX(20px);
  101. -ms-transform: translateX(20px);
  102. transform: translateX(20px);
  103. }
  104.  
  105. .fk-csdn {
  106. color: #000;
  107. font-weight: bold;
  108. font-size: 12px;
  109. vertical-align: middle;
  110. }
  111.  
  112. .fk-box {
  113. position: relative;
  114. height: 30px;
  115. line-height: 30px;
  116. }
  117.  
  118. .fk-box:first-child {
  119. margin-top: 20px;
  120. }
  121. </style>
  122. `
  123. document.querySelector('body').insertAdjacentHTML('afterbegin', insertHtml);
  124. document.querySelector('head').insertAdjacentHTML('afterbegin', insertStyle);
  125.  
  126. var searchEngineCode = '00';
  127. var fixedBtn = document.querySelector('.fixed-btn');
  128. var inputBox = document.querySelector('input[type=checkbox]');
  129. var searchBtn = document;
  130. var searchInput = document;
  131.  
  132. // 1 google 2 duck 3 baidu 4 sogo 5 360 6 doge 7 bing
  133. switch (window.location.host) {
  134. case 'www.google.com':
  135. searchEngineCode = '01';
  136. searchBtn = document.querySelector('.Tg7LZd');
  137. searchInput = document.querySelector('.gLFyf');
  138. break;
  139. case 'duckduckgo.com':
  140. searchEngineCode = '02';
  141. searchBtn = document.querySelector('#search_button');
  142. searchInput = document.querySelector('#search_form_input');
  143. break;
  144. case 'www.baidu.com':
  145. searchEngineCode = '03';
  146. searchBtn = document.querySelector('#su');
  147. searchInput = document.querySelector('#kw');
  148. document.querySelector('#wrapper').insertAdjacentHTML('afterbegin', insertStyle);
  149. break;
  150. case 'www.sogou.com':
  151. searchEngineCode = '04';
  152. searchBtn = document.querySelector('#searchBtn');
  153. searchInput = document.querySelector('#upquery');
  154. break;
  155. case 'www.so.com':
  156. searchEngineCode = '05';
  157. searchBtn = document.querySelector('#su');
  158. searchInput = document.querySelector('#keyword');
  159. break;
  160. case 'www.dogedoge.com':
  161. searchEngineCode = '06';
  162. searchBtn = document.querySelector('#search_button');
  163. searchInput = document.querySelector('#search_form_input');
  164. break;
  165. case 'cn.bing.com':
  166. searchEngineCode = '07';
  167. searchBtn = document.querySelector('#sb_form_go');
  168. searchInput = document.querySelector('#sb_form_q');
  169. break;
  170. case 'www.bing.com':
  171. searchEngineCode = '07';
  172. searchBtn = document.querySelector('#sb_form_go');
  173. searchInput = document.querySelector('#sb_form_q');
  174. break;
  175. default:
  176. searchEngineCode = '00';
  177. console.log('不支持此搜索引擎')
  178. break;
  179. }
  180.  
  181. var isChecked = window.localStorage.getItem('FKCSDNCheck') == 'false' ? false : true;
  182. inputBox.checked = isChecked;
  183.  
  184. var mouseDownClientX, mouseDownClientY, mouseDownOffsetLeft, mouseDownOffsetRight
  185. fixedBtn.addEventListener("mousedown", function(e) {
  186. move = true;
  187. mouseDownClientX = e.clientX;
  188. mouseDownClientY = e.clientY;
  189. mouseDownOffsetLeft = fixedBtn.offsetLeft;
  190. mouseDownOffsetRight = fixedBtn.offsetTop;
  191. });
  192. document.addEventListener("mousemove", function(e) {
  193. if (move == false) {
  194. return;
  195. }
  196. var x = e.clientX;
  197. var y = e.clientY;
  198. fixedBtn.style.left = x - (mouseDownClientX - mouseDownOffsetLeft) + "px";
  199. fixedBtn.style.top = y - (mouseDownClientY - mouseDownOffsetRight) + "px";
  200. });
  201.  
  202. fixedBtn.addEventListener("mouseup", function(e) {
  203. move = false;
  204. });
  205. inputBox.addEventListener('change', function() {
  206. var name = this.getAttribute('name');
  207. var value = this.getAttribute('checked');
  208. var ele = this;
  209. switch (name) {
  210. case 'fk-csdn':
  211. FKCSDN(name, value, ele);
  212. break
  213. case '':
  214. break;
  215. default:
  216. break;
  217. }
  218. })
  219. searchBtn && searchBtn.addEventListener('click', function(e) {
  220. if (searchEngineCode == '00') return;
  221. var searchText = searchInput.value;
  222. var searchArr = searchText.split(' ');
  223. var hasValue = false;
  224. for (var key in searchArr) {
  225. if (searchArr[key] == '-site:csdn.net') {
  226. hasValue = true;
  227. }
  228. }
  229. if (!hasValue && isChecked) {
  230. searchInput.value = searchText + ' -site:csdn.net';
  231. }
  232. })
  233.  
  234. function FKCSDN(name, value, ele) {
  235. if (searchEngineCode == '00') return;
  236. isChecked = ele.checked == true;
  237. window.localStorage.setItem('FKCSDNCheck', isChecked)
  238. if (!isChecked) {
  239. var searchText = searchInput.value;
  240. var searchArr = searchText.split(' ');
  241. var newArr = [];
  242. if (searchArr.includes('-site:csdn.net')) {
  243. searchArr.forEach((value, index) => {
  244. if (value !== '-site:csdn.net') {
  245. newArr.push(value);
  246. }
  247. })
  248. searchInput.value = newArr.join(' ');
  249. }
  250. }
  251. searchBtn.click();
  252.  
  253. }
  254.  
  255. })();