Google Navigation

Navigate through Google Search with a set of hotkeys.

当前为 2021-10-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Navigation
  3. // @name:zh Google 快覽
  4. // @namespace veringsek
  5. // @match http://*.google.com/search*
  6. // @match http://*.google.ad/search*
  7. // @match http://*.google.ae/search*
  8. // @match http://*.google.com.af/search*
  9. // @match http://*.google.com.ag/search*
  10. // @match http://*.google.com.ai/search*
  11. // @match http://*.google.al/search*
  12. // @match http://*.google.am/search*
  13. // @match http://*.google.co.ao/search*
  14. // @match http://*.google.com.ar/search*
  15. // @match http://*.google.as/search*
  16. // @match http://*.google.at/search*
  17. // @match http://*.google.com.au/search*
  18. // @match http://*.google.az/search*
  19. // @match http://*.google.ba/search*
  20. // @match http://*.google.com.bd/search*
  21. // @match http://*.google.be/search*
  22. // @match http://*.google.bf/search*
  23. // @match http://*.google.bg/search*
  24. // @match http://*.google.com.bh/search*
  25. // @match http://*.google.bi/search*
  26. // @match http://*.google.bj/search*
  27. // @match http://*.google.com.bn/search*
  28. // @match http://*.google.com.bo/search*
  29. // @match http://*.google.com.br/search*
  30. // @match http://*.google.bs/search*
  31. // @match http://*.google.bt/search*
  32. // @match http://*.google.co.bw/search*
  33. // @match http://*.google.by/search*
  34. // @match http://*.google.com.bz/search*
  35. // @match http://*.google.ca/search*
  36. // @match http://*.google.cd/search*
  37. // @match http://*.google.cf/search*
  38. // @match http://*.google.cg/search*
  39. // @match http://*.google.ch/search*
  40. // @match http://*.google.ci/search*
  41. // @match http://*.google.co.ck/search*
  42. // @match http://*.google.cl/search*
  43. // @match http://*.google.cm/search*
  44. // @match http://*.google.cn/search*
  45. // @match http://*.google.com.co/search*
  46. // @match http://*.google.co.cr/search*
  47. // @match http://*.google.com.cu/search*
  48. // @match http://*.google.cv/search*
  49. // @match http://*.google.com.cy/search*
  50. // @match http://*.google.cz/search*
  51. // @match http://*.google.de/search*
  52. // @match http://*.google.dj/search*
  53. // @match http://*.google.dk/search*
  54. // @match http://*.google.dm/search*
  55. // @match http://*.google.com.do/search*
  56. // @match http://*.google.dz/search*
  57. // @match http://*.google.com.ec/search*
  58. // @match http://*.google.ee/search*
  59. // @match http://*.google.com.eg/search*
  60. // @match http://*.google.es/search*
  61. // @match http://*.google.com.et/search*
  62. // @match http://*.google.fi/search*
  63. // @match http://*.google.com.fj/search*
  64. // @match http://*.google.fm/search*
  65. // @match http://*.google.fr/search*
  66. // @match http://*.google.ga/search*
  67. // @match http://*.google.ge/search*
  68. // @match http://*.google.gg/search*
  69. // @match http://*.google.com.gh/search*
  70. // @match http://*.google.com.gi/search*
  71. // @match http://*.google.gl/search*
  72. // @match http://*.google.gm/search*
  73. // @match http://*.google.gr/search*
  74. // @match http://*.google.com.gt/search*
  75. // @match http://*.google.gy/search*
  76. // @match http://*.google.com.hk/search*
  77. // @match http://*.google.hn/search*
  78. // @match http://*.google.hr/search*
  79. // @match http://*.google.ht/search*
  80. // @match http://*.google.hu/search*
  81. // @match http://*.google.co.id/search*
  82. // @match http://*.google.ie/search*
  83. // @match http://*.google.co.il/search*
  84. // @match http://*.google.im/search*
  85. // @match http://*.google.co.in/search*
  86. // @match http://*.google.iq/search*
  87. // @match http://*.google.is/search*
  88. // @match http://*.google.it/search*
  89. // @match http://*.google.je/search*
  90. // @match http://*.google.com.jm/search*
  91. // @match http://*.google.jo/search*
  92. // @match http://*.google.co.jp/search*
  93. // @match http://*.google.co.ke/search*
  94. // @match http://*.google.com.kh/search*
  95. // @match http://*.google.ki/search*
  96. // @match http://*.google.kg/search*
  97. // @match http://*.google.co.kr/search*
  98. // @match http://*.google.com.kw/search*
  99. // @match http://*.google.kz/search*
  100. // @match http://*.google.la/search*
  101. // @match http://*.google.com.lb/search*
  102. // @match http://*.google.li/search*
  103. // @match http://*.google.lk/search*
  104. // @match http://*.google.co.ls/search*
  105. // @match http://*.google.lt/search*
  106. // @match http://*.google.lu/search*
  107. // @match http://*.google.lv/search*
  108. // @match http://*.google.com.ly/search*
  109. // @match http://*.google.co.ma/search*
  110. // @match http://*.google.md/search*
  111. // @match http://*.google.me/search*
  112. // @match http://*.google.mg/search*
  113. // @match http://*.google.mk/search*
  114. // @match http://*.google.ml/search*
  115. // @match http://*.google.com.mm/search*
  116. // @match http://*.google.mn/search*
  117. // @match http://*.google.ms/search*
  118. // @match http://*.google.com.mt/search*
  119. // @match http://*.google.mu/search*
  120. // @match http://*.google.mv/search*
  121. // @match http://*.google.mw/search*
  122. // @match http://*.google.com.mx/search*
  123. // @match http://*.google.com.my/search*
  124. // @match http://*.google.co.mz/search*
  125. // @match http://*.google.com.na/search*
  126. // @match http://*.google.com.ng/search*
  127. // @match http://*.google.com.ni/search*
  128. // @match http://*.google.ne/search*
  129. // @match http://*.google.nl/search*
  130. // @match http://*.google.no/search*
  131. // @match http://*.google.com.np/search*
  132. // @match http://*.google.nr/search*
  133. // @match http://*.google.nu/search*
  134. // @match http://*.google.co.nz/search*
  135. // @match http://*.google.com.om/search*
  136. // @match http://*.google.com.pa/search*
  137. // @match http://*.google.com.pe/search*
  138. // @match http://*.google.com.pg/search*
  139. // @match http://*.google.com.ph/search*
  140. // @match http://*.google.com.pk/search*
  141. // @match http://*.google.pl/search*
  142. // @match http://*.google.pn/search*
  143. // @match http://*.google.com.pr/search*
  144. // @match http://*.google.ps/search*
  145. // @match http://*.google.pt/search*
  146. // @match http://*.google.com.py/search*
  147. // @match http://*.google.com.qa/search*
  148. // @match http://*.google.ro/search*
  149. // @match http://*.google.ru/search*
  150. // @match http://*.google.rw/search*
  151. // @match http://*.google.com.sa/search*
  152. // @match http://*.google.com.sb/search*
  153. // @match http://*.google.sc/search*
  154. // @match http://*.google.se/search*
  155. // @match http://*.google.com.sg/search*
  156. // @match http://*.google.sh/search*
  157. // @match http://*.google.si/search*
  158. // @match http://*.google.sk/search*
  159. // @match http://*.google.com.sl/search*
  160. // @match http://*.google.sn/search*
  161. // @match http://*.google.so/search*
  162. // @match http://*.google.sm/search*
  163. // @match http://*.google.sr/search*
  164. // @match http://*.google.st/search*
  165. // @match http://*.google.com.sv/search*
  166. // @match http://*.google.td/search*
  167. // @match http://*.google.tg/search*
  168. // @match http://*.google.co.th/search*
  169. // @match http://*.google.com.tj/search*
  170. // @match http://*.google.tl/search*
  171. // @match http://*.google.tm/search*
  172. // @match http://*.google.tn/search*
  173. // @match http://*.google.to/search*
  174. // @match http://*.google.com.tr/search*
  175. // @match http://*.google.tt/search*
  176. // @match http://*.google.com.tw/search*
  177. // @match http://*.google.co.tz/search*
  178. // @match http://*.google.com.ua/search*
  179. // @match http://*.google.co.ug/search*
  180. // @match http://*.google.co.uk/search*
  181. // @match http://*.google.com.uy/search*
  182. // @match http://*.google.co.uz/search*
  183. // @match http://*.google.com.vc/search*
  184. // @match http://*.google.co.ve/search*
  185. // @match http://*.google.vg/search*
  186. // @match http://*.google.co.vi/search*
  187. // @match http://*.google.com.vn/search*
  188. // @match http://*.google.vu/search*
  189. // @match http://*.google.ws/search*
  190. // @match http://*.google.rs/search*
  191. // @match http://*.google.co.za/search*
  192. // @match http://*.google.co.zm/search*
  193. // @match http://*.google.co.zw/search*
  194. // @match http://*.google.cat/search*
  195. // @match https://*.google.com/search*
  196. // @match https://*.google.ad/search*
  197. // @match https://*.google.ae/search*
  198. // @match https://*.google.com.af/search*
  199. // @match https://*.google.com.ag/search*
  200. // @match https://*.google.com.ai/search*
  201. // @match https://*.google.al/search*
  202. // @match https://*.google.am/search*
  203. // @match https://*.google.co.ao/search*
  204. // @match https://*.google.com.ar/search*
  205. // @match https://*.google.as/search*
  206. // @match https://*.google.at/search*
  207. // @match https://*.google.com.au/search*
  208. // @match https://*.google.az/search*
  209. // @match https://*.google.ba/search*
  210. // @match https://*.google.com.bd/search*
  211. // @match https://*.google.be/search*
  212. // @match https://*.google.bf/search*
  213. // @match https://*.google.bg/search*
  214. // @match https://*.google.com.bh/search*
  215. // @match https://*.google.bi/search*
  216. // @match https://*.google.bj/search*
  217. // @match https://*.google.com.bn/search*
  218. // @match https://*.google.com.bo/search*
  219. // @match https://*.google.com.br/search*
  220. // @match https://*.google.bs/search*
  221. // @match https://*.google.bt/search*
  222. // @match https://*.google.co.bw/search*
  223. // @match https://*.google.by/search*
  224. // @match https://*.google.com.bz/search*
  225. // @match https://*.google.ca/search*
  226. // @match https://*.google.cd/search*
  227. // @match https://*.google.cf/search*
  228. // @match https://*.google.cg/search*
  229. // @match https://*.google.ch/search*
  230. // @match https://*.google.ci/search*
  231. // @match https://*.google.co.ck/search*
  232. // @match https://*.google.cl/search*
  233. // @match https://*.google.cm/search*
  234. // @match https://*.google.cn/search*
  235. // @match https://*.google.com.co/search*
  236. // @match https://*.google.co.cr/search*
  237. // @match https://*.google.com.cu/search*
  238. // @match https://*.google.cv/search*
  239. // @match https://*.google.com.cy/search*
  240. // @match https://*.google.cz/search*
  241. // @match https://*.google.de/search*
  242. // @match https://*.google.dj/search*
  243. // @match https://*.google.dk/search*
  244. // @match https://*.google.dm/search*
  245. // @match https://*.google.com.do/search*
  246. // @match https://*.google.dz/search*
  247. // @match https://*.google.com.ec/search*
  248. // @match https://*.google.ee/search*
  249. // @match https://*.google.com.eg/search*
  250. // @match https://*.google.es/search*
  251. // @match https://*.google.com.et/search*
  252. // @match https://*.google.fi/search*
  253. // @match https://*.google.com.fj/search*
  254. // @match https://*.google.fm/search*
  255. // @match https://*.google.fr/search*
  256. // @match https://*.google.ga/search*
  257. // @match https://*.google.ge/search*
  258. // @match https://*.google.gg/search*
  259. // @match https://*.google.com.gh/search*
  260. // @match https://*.google.com.gi/search*
  261. // @match https://*.google.gl/search*
  262. // @match https://*.google.gm/search*
  263. // @match https://*.google.gr/search*
  264. // @match https://*.google.com.gt/search*
  265. // @match https://*.google.gy/search*
  266. // @match https://*.google.com.hk/search*
  267. // @match https://*.google.hn/search*
  268. // @match https://*.google.hr/search*
  269. // @match https://*.google.ht/search*
  270. // @match https://*.google.hu/search*
  271. // @match https://*.google.co.id/search*
  272. // @match https://*.google.ie/search*
  273. // @match https://*.google.co.il/search*
  274. // @match https://*.google.im/search*
  275. // @match https://*.google.co.in/search*
  276. // @match https://*.google.iq/search*
  277. // @match https://*.google.is/search*
  278. // @match https://*.google.it/search*
  279. // @match https://*.google.je/search*
  280. // @match https://*.google.com.jm/search*
  281. // @match https://*.google.jo/search*
  282. // @match https://*.google.co.jp/search*
  283. // @match https://*.google.co.ke/search*
  284. // @match https://*.google.com.kh/search*
  285. // @match https://*.google.ki/search*
  286. // @match https://*.google.kg/search*
  287. // @match https://*.google.co.kr/search*
  288. // @match https://*.google.com.kw/search*
  289. // @match https://*.google.kz/search*
  290. // @match https://*.google.la/search*
  291. // @match https://*.google.com.lb/search*
  292. // @match https://*.google.li/search*
  293. // @match https://*.google.lk/search*
  294. // @match https://*.google.co.ls/search*
  295. // @match https://*.google.lt/search*
  296. // @match https://*.google.lu/search*
  297. // @match https://*.google.lv/search*
  298. // @match https://*.google.com.ly/search*
  299. // @match https://*.google.co.ma/search*
  300. // @match https://*.google.md/search*
  301. // @match https://*.google.me/search*
  302. // @match https://*.google.mg/search*
  303. // @match https://*.google.mk/search*
  304. // @match https://*.google.ml/search*
  305. // @match https://*.google.com.mm/search*
  306. // @match https://*.google.mn/search*
  307. // @match https://*.google.ms/search*
  308. // @match https://*.google.com.mt/search*
  309. // @match https://*.google.mu/search*
  310. // @match https://*.google.mv/search*
  311. // @match https://*.google.mw/search*
  312. // @match https://*.google.com.mx/search*
  313. // @match https://*.google.com.my/search*
  314. // @match https://*.google.co.mz/search*
  315. // @match https://*.google.com.na/search*
  316. // @match https://*.google.com.ng/search*
  317. // @match https://*.google.com.ni/search*
  318. // @match https://*.google.ne/search*
  319. // @match https://*.google.nl/search*
  320. // @match https://*.google.no/search*
  321. // @match https://*.google.com.np/search*
  322. // @match https://*.google.nr/search*
  323. // @match https://*.google.nu/search*
  324. // @match https://*.google.co.nz/search*
  325. // @match https://*.google.com.om/search*
  326. // @match https://*.google.com.pa/search*
  327. // @match https://*.google.com.pe/search*
  328. // @match https://*.google.com.pg/search*
  329. // @match https://*.google.com.ph/search*
  330. // @match https://*.google.com.pk/search*
  331. // @match https://*.google.pl/search*
  332. // @match https://*.google.pn/search*
  333. // @match https://*.google.com.pr/search*
  334. // @match https://*.google.ps/search*
  335. // @match https://*.google.pt/search*
  336. // @match https://*.google.com.py/search*
  337. // @match https://*.google.com.qa/search*
  338. // @match https://*.google.ro/search*
  339. // @match https://*.google.ru/search*
  340. // @match https://*.google.rw/search*
  341. // @match https://*.google.com.sa/search*
  342. // @match https://*.google.com.sb/search*
  343. // @match https://*.google.sc/search*
  344. // @match https://*.google.se/search*
  345. // @match https://*.google.com.sg/search*
  346. // @match https://*.google.sh/search*
  347. // @match https://*.google.si/search*
  348. // @match https://*.google.sk/search*
  349. // @match https://*.google.com.sl/search*
  350. // @match https://*.google.sn/search*
  351. // @match https://*.google.so/search*
  352. // @match https://*.google.sm/search*
  353. // @match https://*.google.sr/search*
  354. // @match https://*.google.st/search*
  355. // @match https://*.google.com.sv/search*
  356. // @match https://*.google.td/search*
  357. // @match https://*.google.tg/search*
  358. // @match https://*.google.co.th/search*
  359. // @match https://*.google.com.tj/search*
  360. // @match https://*.google.tl/search*
  361. // @match https://*.google.tm/search*
  362. // @match https://*.google.tn/search*
  363. // @match https://*.google.to/search*
  364. // @match https://*.google.com.tr/search*
  365. // @match https://*.google.tt/search*
  366. // @match https://*.google.com.tw/search*
  367. // @match https://*.google.co.tz/search*
  368. // @match https://*.google.com.ua/search*
  369. // @match https://*.google.co.ug/search*
  370. // @match https://*.google.co.uk/search*
  371. // @match https://*.google.com.uy/search*
  372. // @match https://*.google.co.uz/search*
  373. // @match https://*.google.com.vc/search*
  374. // @match https://*.google.co.ve/search*
  375. // @match https://*.google.vg/search*
  376. // @match https://*.google.co.vi/search*
  377. // @match https://*.google.com.vn/search*
  378. // @match https://*.google.vu/search*
  379. // @match https://*.google.ws/search*
  380. // @match https://*.google.rs/search*
  381. // @match https://*.google.co.za/search*
  382. // @match https://*.google.co.zm/search*
  383. // @match https://*.google.co.zw/search*
  384. // @match https://*.google.cat/search*
  385. // @grant none
  386. // @version 0.2.0
  387. // @author veringsek
  388. // @description Navigate through Google Search with a set of hotkeys.
  389. // @description:zh Navigate through Google Search with a set of hotkeys.
  390. // ==/UserScript==
  391.  
  392. (function () {
  393. let css = document.createElement('style');
  394. css.type = 'text/css';
  395. css.innerText = `
  396. :root {
  397. --google-navigation--button-size: 30px;
  398. --google-navigation--stroke-color: black;
  399. --google-navigation--background-color: white;
  400. --google-navigation--button-opacity: 1;
  401. }
  402. .google-navigation--button {
  403. position: absolute;
  404. left: calc(-5 / 3 * var(--google-navigation--button-size));
  405. border: 2px var(--google-navigation--stroke-color) solid;
  406. border-radius: 5px;
  407. background: var(--google-navigation--background-color);
  408. box-shadow: 0px 2px var(--google-navigation--stroke-color);
  409. color: var(--google-navigation--stroke-color) !important;
  410. opacity: var(--google-navigation--button-opacity);
  411. text-decoration: none !important;
  412. width: var(--google-navigation--button-size);
  413. height: var(--google-navigation--button-size);
  414. line-height: var(--google-navigation--button-size);
  415. text-align: center;
  416. font-size: calc(2 / 3 * var(--google-navigation--button-size));
  417. user-select: none;
  418. transition: all 0.1s linear;
  419. animation: spawn 0.2s;
  420. }
  421. .google-navigation--button.keydown {
  422. transform: scale(0.8);
  423. }
  424. .google-navigation--switch {
  425. opacity: 0;
  426. }
  427. .google-navigation--switch.keydown {
  428. opacity: 1;
  429. }
  430. @keyframes spawn {
  431. from {
  432. transform: scale(0);
  433. }
  434. }
  435. `;
  436. document.head.appendChild(css);
  437. })();
  438.  
  439. let timeStart = Date.now();
  440. console.log(`Google Navigation start at ${new Date()}`);
  441.  
  442. let GoogleNavigation = {};
  443. GoogleNavigation.GN_KEYDOWN_TIMER_DURATION = 1000;
  444. GoogleNavigation.GN_KEYDOWN_TIMER_CANCELED = 'GN_KEYDOWN_TIMER_CANCELED';
  445. GoogleNavigation.GN_KEYPRESS_SENSITIVITY = 500;
  446. GoogleNavigation.GN_KEYPRESS_CANCELED = 'GN_KEYPRESS_CANCELED';
  447. GoogleNavigation.GN_BUTTON_SIZE = 30;
  448. GoogleNavigation.GN_BUTTON_SIZE_HALF = GoogleNavigation.GN_BUTTON_SIZE / 2;
  449. GoogleNavigation.keydowns = new Set();
  450. GoogleNavigation.tmrKeydown = undefined;
  451. globalThis.GoogleNavigation = GoogleNavigation;
  452.  
  453. function setCSSVars() {
  454. document.documentElement.style.setProperty(
  455. '--google-navigation--button-size',
  456. `${globalThis.GoogleNavigation.GN_BUTTON_SIZE}px`
  457. );
  458. document.documentElement.style.setProperty(
  459. '--google-navigation--stroke-color',
  460. globalThis.getComputedStyle(document.body)['color']
  461. );
  462. document.documentElement.style.setProperty(
  463. '--google-navigation--background-color',
  464. globalThis.getComputedStyle(document.body)['backgroundColor']
  465. );
  466. }
  467.  
  468. function makeTemplate() {
  469. let template = document.createElement('a');
  470. template.classList.add('google-navigation--button');
  471. template.commandKeydown = function () {
  472. this.classList.add('keydown');
  473. let documentElement = document.documentElement;
  474. documentElement.scroll({
  475. top: this.getBoundingClientRect().top + documentElement.scrollTop - window.innerHeight / 2,
  476. behavior: 'smooth'
  477. });
  478. globalThis.GoogleNavigation.tmrKeydown = globalThis.setTimeout(() => {
  479. globalThis.clearTimeout(globalThis.GoogleNavigation.tmrKeydown);
  480. globalThis.GoogleNavigation.tmrKeydown = globalThis.GoogleNavigation.GN_KEYDOWN_TIMER_CANCELED;
  481. this.classList.remove('keydown');
  482. this.commandCancel();
  483. }, globalThis.GoogleNavigation.GN_KEYDOWN_TIMER_DURATION);
  484. };
  485. template.commandKeyup = function () {
  486. this.classList.remove('keydown');
  487. this.commandPress();
  488. };
  489. template.commandPress = function () {
  490. if (document.getElementById('google-navigation--switch-Control').keydown) {
  491. window.open(this.link, '_blank');
  492. } else {
  493. window.open(this.link, '_self');
  494. }
  495. };
  496. template.commandCancel = function () {
  497. return;
  498. };
  499. globalThis.GoogleNavigation.buttonTemplate = template;
  500. }
  501.  
  502. function getClonedButton(key, link) {
  503. let string = key;
  504. if (Array.isArray(key)) {
  505. [key, string] = key;
  506. }
  507. let cloned = globalThis.GoogleNavigation.buttonTemplate.cloneNode();
  508. cloned.commandKeydown = globalThis.GoogleNavigation.buttonTemplate.commandKeydown;
  509. cloned.commandKeyup = globalThis.GoogleNavigation.buttonTemplate.commandKeyup;
  510. cloned.commandPress = globalThis.GoogleNavigation.buttonTemplate.commandPress;
  511. cloned.commandCancel = globalThis.GoogleNavigation.buttonTemplate.commandCancel;
  512. cloned.innerHTML = string;
  513. cloned.classList.add(`google-navigation--button-${key}`);
  514. if (link) {
  515. cloned.link = link;
  516. cloned.setAttribute('link', link);
  517. }
  518. return cloned;
  519. }
  520.  
  521. function plantButtons() {
  522. let links = [...document.getElementsByClassName('LC20lb')];
  523. let num = 0;
  524. for (let link of links) {
  525. if (getCollapseSectionParent(link)) continue;
  526. if (num >= 10) continue;
  527. let href = link.parentElement.href;
  528. let cloned;
  529. if (num === 0) {
  530. cloned = getClonedButton(['Enter', '⮨'], href);
  531. cloned.classList.add('google-navigation--button-0');
  532. } else {
  533. cloned = getClonedButton(num, href);
  534. }
  535. cloned.style.top = `${link.offsetTop + link.clientHeight / 2 - GoogleNavigation.GN_BUTTON_SIZE_HALF}px`;
  536. link.insertBefore(cloned, link.children[0]);
  537. num += 1;
  538. }
  539.  
  540. let search = document.getElementById('search');
  541. if (search) {
  542. let cloned = getClonedButton(['Control', 'CTRL']);
  543. cloned.id = 'google-navigation--switch-Control';
  544. cloned.classList.add('google-navigation--switch');
  545. cloned.style.left = '-150px';
  546. cloned.style.width = '80px';
  547. cloned.style.fontWeight = 'bold';
  548. cloned.keydown = false;
  549. cloned.commandKeydown = function () {
  550. this.tmrKeypress = globalThis.setTimeout(() => {
  551. this.tmrKeypress = globalThis.GoogleNavigation.GN_KEYPRESS_CANCELED;
  552. }, globalThis.GoogleNavigation.GN_KEYPRESS_SENSITIVITY);
  553. };
  554. cloned.commandKeyup = function () {
  555. if (this.tmrKeypress === globalThis.GoogleNavigation.GN_KEYPRESS_CANCELED) {
  556. this.tmrKeypress = undefined;
  557. } else if (this.tmrKeypress) {
  558. this.keydown = !this.keydown;
  559. if (this.keydown) {
  560. this.classList.add('keydown');
  561. } else {
  562. this.classList.remove('keydown');
  563. }
  564. }
  565. };
  566. search.insertBefore(cloned, search.children[0]);
  567. }
  568.  
  569. let btnWiki = document.getElementsByClassName('ruhjFe')[0];
  570. if (btnWiki) {
  571. let cloned = getClonedButton(['p', 'P'], btnWiki.href);
  572. let card = getWidgetParent(btnWiki);
  573. card.style.position = 'relative';
  574. card.insertBefore(cloned, card.children[0]);
  575. }
  576.  
  577. let translatorWidgetMain = document.getElementById('tw-main');
  578. let translatorWidgetSourceTextTA = document.getElementById('tw-source-text-ta');
  579. if (translatorWidgetMain && translatorWidgetSourceTextTA) {
  580. let cloned = getClonedButton(['t', 'T']);
  581. let rectMain = translatorWidgetMain.getBoundingClientRect();
  582. let rectText = translatorWidgetSourceTextTA.getBoundingClientRect();
  583. let top = (rectText.bottom + rectText.top) / 2 - rectMain.top - GoogleNavigation.GN_BUTTON_SIZE_HALF;
  584. cloned.style.top = `${top}px`;
  585. cloned.commandPress = function () {
  586. document.getElementById('tw-source-text-ta').focus();
  587. };
  588. translatorWidgetMain.style.position = 'relative';
  589. translatorWidgetMain.insertBefore(cloned, translatorWidgetMain.children[0]);
  590. }
  591.  
  592. let calculatorWidget = document.getElementById('cwos')?.parentElement?.parentElement;
  593. if (calculatorWidget) {
  594. let calculatorWidgetParent = getWidgetParent(calculatorWidget);
  595. let cloned = getClonedButton(['t', 'T']);
  596. let top = calculatorWidgetParent.clientHeight / 2 - globalThis.GoogleNavigation.GN_BUTTON_SIZE_HALF;
  597. cloned.style.top = `${top}px`;
  598. cloned.commandPress = function () {
  599. document.getElementById('cwos').parentElement.parentElement.focus();
  600. };
  601. calculatorWidgetParent.insertBefore(cloned, calculatorWidgetParent.children[0]);
  602. }
  603.  
  604. // let currentPage = document.getElementById('top_nav')?.querySelector('[aria-current=page]');
  605. // if (currentPage) {
  606. // let nav = currentPage.parentElement;
  607. // let next = currentPage.nextSibling;
  608. // if (!next) next = nav.children[0];
  609. // let cloned = getClonedButton(['n', 'N'], next.getElementsByTagName('a')[0]?.href);
  610. // cloned.style.left = '130px';
  611. // nav.insertBefore(cloned, nav.children[0]);
  612. // }
  613.  
  614. let pnprev = document.getElementById('pnprev');
  615. if (pnprev) {
  616. let cloned = getClonedButton([',', ','], pnprev.href);
  617. cloned.style.top = '-3px';
  618. cloned.style.left = '-40px';
  619. pnprev.style.position = 'relative';
  620. pnprev.insertBefore(cloned, pnprev.children[0]);
  621. }
  622. let pnnext = document.getElementById('pnnext');
  623. if (pnnext) {
  624. let cloned = getClonedButton(['.', '‧'], pnnext.href);
  625. cloned.style.top = '-3px';
  626. cloned.style.left = 'calc(100% + 5px)';
  627. pnnext.style.position = 'relative';
  628. pnnext.insertBefore(cloned, pnnext.children[0]);
  629. }
  630. }
  631.  
  632. function isCommanding() {
  633. let target = document.activeElement;
  634. return !(['INPUT', 'TEXTAREA'].includes(target.tagName) || target.classList.contains('jlkklc'));
  635. }
  636.  
  637. function cssVar(name) {
  638. return globalThis.getComputedStyle(document.documentElement).getPropertyValue(name);
  639. }
  640.  
  641. function getWidgetParent(element) {
  642. let node = element;
  643. do {
  644. let computedStyle = globalThis.getComputedStyle(node);
  645. let borderWidth = computedStyle.borderWidth;
  646. if (borderWidth !== '' && !(/^0\D*/.test(borderWidth))) {
  647. return node;
  648. }
  649. node = node.parentElement;
  650. } while (node !== document.body);
  651. return null;
  652. }
  653.  
  654. function getCollapseSectionParent(element) {
  655. let node = element;
  656. do {
  657. if (node.children.length > 0) {
  658. let computedStyle = globalThis.getComputedStyle(node.children[0]);
  659. let borderWidth = computedStyle.borderWidth;
  660. if (borderWidth === '1px 0px 0px') return node;
  661. }
  662. node = node.parentElement;
  663. } while (node !== document.body);
  664. return null;
  665. }
  666.  
  667. function assignEventListeners() {
  668. let onFocusChanged = function (ev) {
  669. document.documentElement.style.setProperty(
  670. '--google-navigation--button-opacity',
  671. isCommanding() ? 1 : 0.5
  672. );
  673. };
  674. window.addEventListener('focus', onFocusChanged, true);
  675. window.addEventListener('blur', onFocusChanged, true);
  676.  
  677. document.body.addEventListener('keydown', function (ev) {
  678. if (globalThis.GoogleNavigation.keydowns.has(ev.key)) return;
  679. globalThis.GoogleNavigation.keydowns.add(ev.key);
  680. if (isCommanding()) {
  681. let button = document.getElementsByClassName(`google-navigation--button-${ev.key}`)[0];
  682. if (button) {
  683. button.commandKeydown();
  684. }
  685. }
  686. });
  687.  
  688. document.body.addEventListener('keyup', function (ev) {
  689. globalThis.GoogleNavigation.keydowns.delete(ev.key);
  690. if (globalThis.GoogleNavigation.tmrKeydown === globalThis.GoogleNavigation.GN_KEYDOWN_TIMER_CANCELED) {
  691. globalThis.GoogleNavigation.tmrKeydown = undefined;
  692. return;
  693. }
  694. globalThis.clearTimeout(globalThis.GoogleNavigation.tmrKeydown);
  695. globalThis.GoogleNavigation.tmrKeydown = undefined;
  696. if (isCommanding()) {
  697. let button = document.getElementsByClassName(`google-navigation--button-${ev.key}`)[0];
  698. if (button) {
  699. button.commandKeyup();
  700. }
  701. }
  702. });
  703. }
  704.  
  705. setCSSVars();
  706. makeTemplate();
  707. plantButtons();
  708. assignEventListeners();
  709.  
  710. console.log(`Google Navigation end at ${new Date()}`);
  711. console.log(`Google Navigation consumed ${Date.now() - timeStart} ms. `);

QingJ © 2025

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