FKCSDN

try to take over the world!

当前为 2020-04-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name FKCSDN
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description try to take over the world!
  6. // @author Bryce
  7. // @include *://www.baidu.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12. (function() {
  13. 'use strict';
  14. var move = false
  15. var insertHtml = `
  16. <div style="" class="fixed-btn">
  17. <div class="fk-box">
  18. <div class="fk-csdn">FK CSDN</div>
  19. <label class="fk-item">
  20. <input type="checkbox" name="fk-csdn" checked="true">
  21. <div class="slider round"></div>
  22. </label>
  23. </div>
  24. </div>`
  25.  
  26. var insertStyle = `.fixed-btn {
  27. -webkit-user-select: none;
  28. position: fixed;
  29. top: 50px;
  30. right: 50px;
  31. border-radius: 50px;
  32. width: 100px;
  33. height: 100px;
  34. background-color: #f2f8ff;
  35. }
  36.  
  37. .btn {
  38. /*background: #237bff;*/
  39. position: absolute;
  40. background-color: #237bff;
  41. border-radius: 20px;
  42. border: none;
  43. }
  44.  
  45. .slider.round {
  46. border-radius: 34px;
  47. }
  48.  
  49. .slider {
  50. position: absolute;
  51. cursor: pointer;
  52. width: 40px;
  53. height: 20px;
  54. top: 0;
  55. left: 0;
  56. right: 0;
  57. bottom: 0;
  58. background-color: #ccc;
  59. -webkit-transition: .4s;
  60. transition: .4s;
  61. }
  62.  
  63. .slider.round:before {
  64. border-radius: 50%;
  65. }
  66.  
  67. .slider:before {
  68. position: absolute;
  69. content: "";
  70. height: 16px;
  71. width: 16px;
  72. left: 2px;
  73. bottom: 2px;
  74. background-color: white;
  75. -webkit-transition: .4s;
  76. transition: .4s;
  77. }
  78.  
  79. .fk-item input {
  80. display: none;
  81. }
  82.  
  83. .fk-item {
  84. position: absolute;
  85. right: 0;
  86. top: 10px;
  87. display: inline-block;
  88. vertical-align: middle;
  89. width: 40px;
  90. height: 20px;
  91. top: 6px;
  92. }
  93.  
  94. input:checked+.slider {
  95. background-color: #2196F3;
  96. }
  97.  
  98. input:checked+.slider:before {
  99. -webkit-transform: translateX(20px);
  100. -ms-transform: translateX(20px);
  101. transform: translateX(20px);
  102. }
  103.  
  104. .fk-csdn {
  105. color: #000;
  106. font-weight: bold;
  107. font-size: 12px;
  108. vertical-align: middle;
  109. }
  110.  
  111. .fk-box {
  112. position: relative;
  113. height: 30px;
  114. line-height: 30px;
  115. }
  116.  
  117. .fk-box:first-child {
  118. margin-top: 20px;
  119. }`
  120.  
  121. var styleDom = document.createElement('style');
  122. styleDom.type='text/css';
  123. if (styleDom.styleSheet) {
  124. styleDom.styleSheet.cssText = insertStyle;
  125. } else {
  126. styleDom.innerHTML = insertStyle;
  127. }
  128. document.querySelector('#wrapper').appendChild(styleDom);
  129. document.querySelector('#head').insertAdjacentHTML('beforeEnd', insertHtml);
  130.  
  131. var fixedBtn = document.querySelector('.fixed-btn');
  132. var inputBox = document.querySelector('input[type=checkbox]');
  133. var searchBtn = document.querySelector('#su');
  134. var isChecked = true;
  135. fixedBtn.addEventListener("mousedown", function(e) {
  136. move = true;
  137. });
  138. fixedBtn.addEventListener("mousemove", function(e) {
  139. var x = e.clientX;
  140. var y = e.clientY;
  141. if (move) {
  142. fixedBtn.style.left = x - fixedBtn.clientWidth / 2 + "px";
  143. fixedBtn.style.top = y - fixedBtn.clientHeight / 2 + "px";
  144. }
  145. });
  146.  
  147. document.addEventListener("mouseup", function(e) {
  148. move = false;
  149. });
  150. inputBox.addEventListener('change', function() {
  151. var name = this.getAttribute('name');
  152. var value = this.getAttribute('checked');
  153. var ele = this;
  154. switch (name) {
  155. case 'fk-csdn':
  156. FKCSDN(name, value, ele);
  157. break
  158. case '':
  159. break;
  160. default:
  161. break;
  162. }
  163. })
  164. searchBtn.addEventListener('click', function() {
  165. var searchInput = document.querySelector('#kw');
  166. var searchText = searchInput.value;
  167. var searchArr = searchText.split(' ');
  168. var hasValue = false;
  169. for (var key in searchArr) {
  170. if (searchArr[key] == '-site:csdn.net') {
  171. hasValue = true;
  172. }
  173. }
  174. if (!hasValue && isChecked) {
  175. searchInput.value = searchText + ' -site:csdn.net';
  176. }
  177. })
  178. function FKCSDN(name, value, ele) {
  179. isChecked = ele.checked;
  180. if(!ele.checked){
  181. var searchInput = document.querySelector('#kw');
  182. var searchText = searchInput.value;
  183. var searchArr = searchText.split(' ');
  184. var newArr = [];
  185. if(searchArr.includes('-site:csdn.net')){
  186. searchArr.forEach((value,index)=> {
  187. if(value !=='-site:csdn.net'){
  188. newArr.push(value);
  189. }
  190. })
  191. searchInput.value = newArr.join(' ');
  192. }
  193. }
  194. }
  195.  
  196. })();