URL Modifier for Search Engines

Modify URLs in search results of search engines

当前为 2024-01-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name URL Modifier for Search Engines
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.2.1
  5. // @description Modify URLs in search results of search engines
  6. // @author Domenic
  7. // @match *://www.google.com/search?*q=*
  8. // @match *://yandex.com/search/?text=*
  9. // @match *://yandex.ru/search/?text=*
  10. // @match *://search.disroot.org/search*
  11. // @match *://searx.tiekoetter.com/search*
  12. // @match *://search.bus-hit.me/search*
  13. // @match *://search.inetol.net/search*
  14. // @match *://priv.au/search*
  15. // @match *://searx.be/search*
  16. // @match *://searxng.site/search*
  17. // @match *://search.hbubli.cc/search*
  18. // @match *://search.im-in.space/search*
  19. // @match *://opnxng.com/search*
  20. // @match *://search.upinmars.com/search*
  21. // @match *://search.sapti.me/search*
  22. // @match *://freesearch.club/search*
  23. // @match *://xo.wtf/search*
  24. // @match *://www.gruble.de/search*
  25. // @match *://searx.tuxcloud.net/search*
  26. // @match *://baresearch.org/search*
  27. // @match *://searx.daetalytica.io/search*
  28. // @match *://etsi.me/search*
  29. // @match *://search.leptons.xyz/search*
  30. // @match *://search.rowie.at/search*
  31. // @match *://search.mdosch.de/search*
  32. // @match *://searx.catfluori.de/search*
  33. // @match *://searx.si/search*
  34. // @match *://searx.namejeff.xyz/search*
  35. // @match *://search.itstechtime.com/search*
  36. // @match *://s.mble.dk/search*
  37. // @match *://searx.kutay.dev/search*
  38. // @match *://ooglester.com/search*
  39. // @match *://searx.ox2.fr/search*
  40. // @match *://searx.techsaviours.org/search*
  41. // @match *://searx.perennialte.ch/search*
  42. // @match *://s.trung.fun/search*
  43. // @match *://search.in.projectsegfau.lt/search*
  44. // @match *://search.projectsegfau.lt/search*
  45. // @match *://darmarit.org/searx/search*
  46. // @match *://searx.lunar.icu/search*
  47. // @match *://nyc1.sx.ggtyler.dev/search*
  48. // @match *://search.rhscz.eu/search*
  49. // @match *://paulgo.io/search*
  50. // @match *://northboot.xyz/search*
  51. // @match *://searx.zhenyapav.com/search*
  52. // @match *://searxng.ch/search*
  53. // @match *://copp.gg/search*
  54. // @match *://searx.sev.monster/search*
  55. // @match *://searx.oakleycord.dev/search*
  56. // @match *://searx.juancord.xyz/search*
  57. // @match *://searx.work/search*
  58. // @match *://search.ononoki.org/search*
  59. // @match *://search.demoniak.ch/search*
  60. // @match *://searx.cthd.icu/search*
  61. // @match *://searx.fmhy.net/search*
  62. // @match *://searx.headpat.exchange/search*
  63. // @match *://sex.finaltek.net/search*
  64. // @match *://search.gcomm.ch/search*
  65. // @match *://search.smnz.de/search*
  66. // @match *://searx.ankha.ac/search*
  67. // @match *://search.lvkaszus.pl/search*
  68. // @match *://searx.nobulart.com/search*
  69. // @match *://sx.t-1.org/search*
  70. // @match *://www.jabber-germany.de/searx/search*
  71. // @match *://sx.catgirl.cloud/search*
  72. // @match *://www.startpage.com/search*
  73. // @match *://www.startpage.com/sp/search*
  74. // @match *://search.brave.com/search*
  75. // @match *://duckduckgo.com
  76. // @match *://duckduckgo.com/?*q=*
  77. // @match *://www.qwant.com/?q=*
  78. // @match *://www.ecosia.org/search?*
  79. // @match *://swisscows.com/*/web?query=*
  80. // @match *://metager.org/meta/meta.ger3*
  81. // @match *://metager.de/meta/meta.ger3*
  82. // @match *://4get.ca/web?s=*
  83. // @match *://4get.silly.computer/web?s=*
  84. // @match *://4get.plunked.party/web?s=*
  85. // @match *://4get.konakona.moe/web?s=*
  86. // @match *://4get.sijh.net/web?s=*
  87. // @match *://4get.hbubli.cc/web?s=*
  88. // @match *://4get.perennialte.ch/web?s=*
  89. // @match *://4get.zzls.xyz/web?s=*
  90. // @match *://4getus.zzls.xyz/web?s=*
  91. // @match *://4get.seitan-ayoub.lol/web?s=*
  92. // @match *://4get.dcs0.hu/web?s=*
  93. // @match *://4get.psily.garden/web?s=*
  94. // @match *://4get.lvkaszus.pl/web?s=*
  95. // @match *://4get.kizuki.lol/web?s=*
  96. // @match *://stract.com/search?*
  97. // @match *://www.etools.ch/searchSubmit.do*
  98. // @match *://www.etools.ch/mobileSearch.do*
  99. // @match *://www.mojeek.com/search?q=*
  100. // @match *://yep.com/web?q=*
  101. // @match *://www.torry.io/search*
  102. // @grant none
  103. // @run-at document-end
  104. // @license GPL-2.0-only
  105. // ==/UserScript==
  106.  
  107. // TODO: Modification of URL of widgets
  108.  
  109. (function() {
  110. 'use strict';
  111.  
  112. // Define URL modification rules with precompiled regex
  113. const urlModificationRules = [
  114. {
  115. matchRegex: new RegExp(/^https?:\/\/www\.reddit\.com(.*)/),
  116. replaceWith: 'https://old.reddit.com$1'
  117. },
  118. {
  119. matchRegex: new RegExp(/^https?:\/\/www\.quora\.com\/((?=.*-)[\w-]+$|profile\/.*)/),
  120. replaceWith: 'https://quetre.iket.me/$1'
  121. },
  122. {
  123. matchRegex: new RegExp(/^https?:\/\/twitter\.com\/([A-Za-z_][\w]+)(\/status\/(\d+))?.*/),
  124. replaceWith: 'https://nitter.net/$1$2'
  125. },
  126. // {
  127. // matchRegex: new RegExp(/^https?:\/\/stackoverflow\.com(\/questions\/\d+\/[\w-]+)/),
  128. // replaceWith: 'https://code.whatever.social$1'
  129. // },
  130. {
  131. matchRegex: new RegExp(/^https?:\/\/(?:www\.)?youtube\.com\/(@[\w-]+|watch\?(?:[\w=&]+&)?v=[\w-]+|playlist\?list=[\w-]+)/),
  132. replaceWith: 'https://iv.datura.network/$1'
  133. // replaceWith: 'https://piped.video/$1'
  134. },
  135. {
  136. matchRegex: new RegExp(/^https?:\/\/(?:en\.?m?|simple)\.wikipedia\.org\/wiki\/(?!Special:Search)(.*)/),
  137. replaceWith: 'https://www.wikiwand.com/en/$1'
  138. },
  139. {
  140. matchRegex: new RegExp(/^https?:\/\/zh\.?m?\.wikipedia\.org\/(?:zh-hans|wiki)\/(.*)/),
  141. replaceWith: 'https://www.wikiwand.com/zh-hans/$1'
  142. },
  143. {
  144. matchRegex: new RegExp(/^https?:\/\/((?!test)[a-z]+)\.?m?\.wikipedia\.org\/(?:[a-z]+|wiki)\/(?!Special:Search)(.*)/),
  145. replaceWith: 'https://www.wikiwand.com/$1/$2'
  146. },
  147. {
  148. matchRegex: new RegExp(/^https?:\/\/((?:(?:\w+\.)?medium|towardsdatascience)\.com\/(?=.*-)(?:[\w\/-]+$|[\w@.]+\/[\w-]+$))/),
  149. replaceWith: 'https://freedium.cfd/https://$1'
  150. },
  151. {
  152. matchRegex: new RegExp(/^https?:\/\/(?:m|www)\.imdb\.com(.*)/),
  153. replaceWith: 'https://ld.vern.cc$1'
  154. },
  155. {
  156. matchRegex: new RegExp(/^https?:\/\/www\.goodreads\.com\/((?:(?:[a-z]+\/)?book\/show|work\/quotes|series|author\/show)\/[\w.-]+)/),
  157. replaceWith: 'https://bl.vern.cc/$1'
  158. },
  159. {
  160. matchRegex: new RegExp(/^https?:\/\/www\.urbandictionary\.com\/(define\.php\?term=.*)/),
  161. replaceWith: 'https://rd.vern.cc/$1'
  162. },
  163. {
  164. matchRegex: new RegExp(/^https?:\/\/imgur\.com\/(a\/)?((?!gallery)\w+)/),
  165. replaceWith: 'https://rimgo.totaldarkness.net/a/$1$2'
  166. },
  167. {
  168. matchRegex: new RegExp(/^https?:\/\/www\.pixiv\.net\/(?:[a-z]+\/)?(artworks\/\d+|tags\/\w+|users\/\d+).*/),
  169. replaceWith: 'https://pixivfe.exozy.me/$1'
  170. },
  171. {
  172. matchRegex: new RegExp(/^https?:\/\/genius\.com\/((?=[\w-]+lyrics|search\?q=).*)/),
  173. replaceWith: 'https://dm.vern.cc/$1'
  174. },
  175. {
  176. matchRegex: new RegExp(/^https?:\/\/(?:(?:.*)arxiv\.org\/pdf|arxiv-export-lb\.library\.cornell\.edu\/(?:pdf|abs))\/(\d{4}\.\d{4,5}(v\d)?)(?:.*)/),
  177. replaceWith: 'https://arxiv.org/abs/$1'
  178. },
  179. {
  180. matchRegex: new RegExp(/^https?:\/\/(ieeexplore\.ieee\.org\/document\/\d+)\//),
  181. replaceWith: 'https://$1'
  182. },
  183. {
  184. matchRegex: new RegExp(/^https?:\/\/github\.ink(.*)/),
  185. replaceWith: 'https://github.com$1'
  186. },
  187. {
  188. matchRegex: new RegExp(/^https?:\/\/www\.npr\.org\/(?:\d{4}\/\d{2}\/\d{2}|sections)\/(?:[A-Za-z-]+\/\d{4}\/\d{2}\/\d{2}\/)?(\d+)\/.*/),
  189. replaceWith: 'https://text.npr.org/$1'
  190. },
  191. {
  192. matchRegex: new RegExp(/^https?:\/\/(?:[a-z]+)\.slashdot\.org(.*)/),
  193. replaceWith: 'https://slashdot.org$1'
  194. },
  195. {
  196. matchRegex: new RegExp(/^https?:\/\/www\.snopes\.com(.*)/),
  197. replaceWith: 'https://sd.vern.cc$1'
  198. }
  199. // Add more rules here as needed
  200. ];
  201.  
  202. // Define enhanced selector rules for each search engine
  203. const selectorRules = {
  204. 'google': [
  205. {
  206. selector: 'div.MjjYud div.yuRUbf div span a',
  207. childSelector: 'div.byrV5b cite',
  208. updateChildText: true,
  209. useTopLevelDomain: true, // Flag for using top-level domain
  210. containProtocol: true,
  211. displayMethod: 1
  212. },
  213. {
  214. // Selector for sub-results
  215. selector: 'div.MjjYud div.HiHjCd a'
  216. }
  217. // ... [Other rules for Google]
  218. ],
  219. 'yandex': [
  220. {
  221. selector: 'ul#search-result li div.Organic-Subtitle div a',
  222. updateChildText: true,
  223. containProtocol: false,
  224. displayMethod: 1,
  225. },
  226. {
  227. selector: 'ul#search-result li div.Organic div a',
  228. }
  229. ],
  230. 'searx': [
  231. {
  232. selector: 'article a.url_wrapper',
  233. childSelector: 'span span',
  234. updateChildText: true,
  235. useTopLevelDomain: true,
  236. containProtocol: true,
  237. displayMethod: 1,
  238. multiElementsForUrlDisplay: true
  239. },
  240. {
  241. selector: 'h3 a'
  242. }
  243. ],
  244. 'startpage': [
  245. {
  246. selector: 'a.w-gl__result-url.result-link',
  247. updateText: true,
  248. displayMethod: 2
  249. },
  250. {
  251. selector: 'a.w-gl__result-title.result-link'
  252. }
  253. ],
  254. 'brave': [
  255. {
  256. selector: 'a.h.svelte-1dihpoi',
  257. childSelector: 'cite.snippet-url.svelte-1ygzem6 span',
  258. updateChildText: true,
  259. containProtocol: false,
  260. displayMethod: 1,
  261. multiElementsForUrlDisplay: true
  262. }
  263. ],
  264. 'duckduckgo': [
  265. {
  266. selector: 'a.eVNpHGjtxRBq_gLOfGDr.LQNqh2U1kzYxREs65IJu'
  267. },
  268. {
  269. selector: 'a.Rn_JXVtoPVAFyGkcaXyK',
  270. childSelector: 'span',
  271. updateChildText: true,
  272. containProtocol: true,
  273. displayMethod: 1,
  274. multiElementsForUrlDisplay: true
  275. },
  276. {
  277. // Selector for sub-results
  278. selector: 'ul.b269SZlC2oyR13Fcc4Iy li a.f3uDrYrWF3Exrfp1m3Og'
  279. }
  280. ],
  281. 'qwant': [
  282. {
  283. selector: 'div._35zId._3A7p7.RMB_d.eoseI a.external'
  284. },
  285. {
  286. selector: 'div._35zId._3WA-c a.external',
  287. childSelector: 'span',
  288. updateChildText: true,
  289. containProtocol: false,
  290. displayMethod: 1,
  291. multiElementsForUrlDisplay: true
  292. },
  293. {
  294. // Selector for sub-results
  295. selector: 'div._12BMd div._2-LMx._2E8gc._16lFV.Ks7KS.tCpbb.m_hqb a.external'
  296. }
  297. ],
  298. 'ecosia': [
  299. {
  300. selector: 'div.mainline__result-wrapper div.result__header div.result__info a',
  301. childSelector: 'span span',
  302. updateChildText: true,
  303. containProtocol: true,
  304. displayMethod: 1,
  305. multiElementsForUrlDisplay: true
  306. },
  307. {
  308. selector: 'div.mainline__result-wrapper div.result__header div.result__title a'
  309. },
  310. {
  311. selector: 'div.mainline__result-wrapper div.result__extra-content div ul li a'
  312. },
  313. {
  314. selector: 'div.mainline__result-wrapper div.entity-links ul li a'
  315. },
  316. {
  317. selector: 'aside.sidebar article div.entity-links ul li a'
  318. }
  319. ],
  320. 'swisscows': [
  321. {
  322. selector: 'article.item-web a',
  323. updateText: true,
  324. containProtocol: false,
  325. displayMethod: 1
  326. }
  327. ],
  328. 'metager': [
  329. {
  330. selector: 'h2.result-title a'
  331. },
  332. {
  333. selector: 'div.result-subheadline a',
  334. updateText: true,
  335. displayMethod: 3
  336. }
  337. ],
  338. '4get': [
  339. {
  340. selector: 'div.text-result a.hover'
  341. },
  342. {
  343. selector: 'div.text-result div.sublinks a'
  344. },
  345. {
  346. selector: 'div.right-wrapper div.answer-wrapper div.answer div.answer-title a.answer-title'
  347. }
  348. ],
  349. 'stract': [
  350. {
  351. selector: 'div.grid div div div div div a',
  352. updateText: true,
  353. displayMethod: 2
  354. },
  355. {
  356. selector: 'div.grid div div div div a'
  357. }
  358. ],
  359. 'etools': [
  360. {
  361. // searchSubmit.do
  362. selector: 'td.record a.title'
  363. },
  364. {
  365. // mobileSearch.do
  366. selector: 'p a.title'
  367. }
  368. ],
  369. 'mojeek': [
  370. {
  371. selector: 'ul.results-standard li a.ob',
  372. childSelector: 'span.url',
  373. updateChildText: true,
  374. useTopLevelDomain: true,
  375. containProtocol: true,
  376. displayMethod: 1
  377. },
  378. {
  379. selector: 'div.infobox.infobox-top a'
  380. }
  381. ],
  382. 'yep': [
  383. {
  384. selector: 'div.css-102xgmn-card div div a',
  385. childSelector: 'div span',
  386. updateChildText: true,
  387. containProtocol: false,
  388. displayMethod: 1
  389. }
  390. ],
  391. 'torry': [
  392. {
  393. selector: 'div.searpList p a.toranclick',
  394. updateText: true,
  395. displayMethod: 2
  396. },
  397. {
  398. selector: 'div.searpList div h2 a.toranclick',
  399. },
  400. {
  401. selector: 'div.searpList ul li a',
  402. }
  403. ]
  404. // Additional search engines can be defined here...
  405. };
  406.  
  407. // User-defined list of search engine instance URLs
  408. const searchEngines = {
  409. 'google': {
  410. hosts: ['google.com'],
  411. // search results container
  412. // you can ignore this parameter if you don't want to set it, just delete it
  413. // defult value is 'body'
  414. resultContainerSelectors: ['div.GyAeWb#rcnt']
  415. },
  416. 'yandex': {
  417. hosts: [
  418. 'yandex.com',
  419. 'yandex.ru'
  420. ],
  421. resultContainerSelectors: ['div.main__container']
  422. },
  423. 'searx': {
  424. hosts: [
  425. 'search.disroot.org',
  426. 'searx.tiekoetter.com',
  427. 'search.bus-hit.me',
  428. 'search.inetol.net',
  429. 'priv.au',
  430. 'searx.be',
  431. 'searxng.site',
  432. 'search.hbubli.cc',
  433. 'search.im-in.space',
  434. 'opnxng.com',
  435. 'search.upinmars.com',
  436. 'search.sapti.me',
  437. 'freesearch.club',
  438. 'xo.wtf',
  439. 'www.gruble.de',
  440. 'searx.tuxcloud.net',
  441. 'baresearch.org',
  442. 'searx.daetalytica.io',
  443. 'etsi.me',
  444. 'search.leptons.xyz',
  445. 'search.rowie.at',
  446. 'search.mdosch.de',
  447. 'searx.catfluori.de',
  448. 'searx.si',
  449. 'searx.namejeff.xyz',
  450. 'search.itstechtime.com',
  451. 's.mble.dk',
  452. 'searx.kutay.dev',
  453. 'ooglester.com',
  454. 'searx.ox2.fr',
  455. 'searx.techsaviours.org',
  456. 'searx.perennialte.ch',
  457. 's.trung.fun',
  458. 'search.in.projectsegfau.lt',
  459. 'search.projectsegfau.lt',
  460. 'darmarit.org',
  461. 'searx.lunar.icu',
  462. 'nyc1.sx.ggtyler.dev',
  463. 'search.rhscz.eu',
  464. 'paulgo.io',
  465. 'northboot.xyz',
  466. 'searx.zhenyapav.com',
  467. 'searxng.ch',
  468. 'copp.gg',
  469. 'searx.sev.monster',
  470. 'searx.oakleycord.dev',
  471. 'searx.juancord.xyz',
  472. 'searx.work',
  473. 'search.ononoki.org',
  474. 'search.demoniak.ch',
  475. 'searx.cthd.icu',
  476. 'searx.fmhy.net',
  477. 'searx.headpat.exchange',
  478. 'sex.finaltek.net',
  479. 'search.gcomm.ch',
  480. 'search.smnz.de',
  481. 'searx.ankha.ac',
  482. 'search.lvkaszus.pl',
  483. 'searx.nobulart.com',
  484. 'sx.t-1.org',
  485. 'www.jabber-germany.de',
  486. 'sx.catgirl.cloud'
  487. ],
  488. resultContainerSelectors: [
  489. 'main#main_results'
  490. // 'maindiv#main_results div#urls'
  491. // 'div#sidebar div#infoboxes'
  492. ]
  493. },
  494. 'startpage': {
  495. hosts: ['startpage.com'],
  496. resultContainerSelectors: [
  497. 'div.show-results'
  498. // 'div.sidebar-results'
  499. ]
  500. },
  501. 'brave': {
  502. hosts: ['search.brave.com'],
  503. resultContainerSelectors: [
  504. 'main.main-column'
  505. // 'aside.sidebar'
  506. ]
  507. },
  508. 'duckduckgo': {
  509. hosts: ['duckduckgo.com'],
  510. resultContainerSelectors: [
  511. 'section[data-testid="mainline"][data-area="mainline"]'
  512. // 'section[data-testid="sidebar"][data-area="sidebar"]'
  513. ]
  514. },
  515. 'qwant': {
  516. hosts: ['qwant.com'],
  517. resultContainerSelectors: [
  518. 'div._35zId'
  519. ]
  520. },
  521. 'ecosia': {
  522. hosts: ['ecosia.org'],
  523. resultContainerSelectors: [
  524. 'section.mainline.web__mainline',
  525. 'aside.sidebar.web__sidebar'
  526. ]
  527. },
  528. 'swisscows': {
  529. hosts: ['swisscows.com'],
  530. resultContainerSelectors: [
  531. 'section.container.page-results'
  532. ]
  533. },
  534. 'metager': {
  535. hosts: [
  536. 'metager.org',
  537. 'metager.de'
  538. ],
  539. resultContainerSelectors: ['div#results']
  540. },
  541. '4get': {
  542. hosts: [
  543. '4get.ca',
  544. '4get.silly.computer',
  545. '4get.plunked.party',
  546. '4get.konakona.moe',
  547. '4get.sijh.net',
  548. '4get.hbubli.cc',
  549. '4get.perennialte.ch',
  550. '4get.zzls.xyz',
  551. '4getus.zzls.xyz',
  552. '4get.seitan-ayoub.lol',
  553. '4get.dcs0.hu',
  554. '4get.psily.garden',
  555. '4get.lvkaszus.pl',
  556. '4get.kizuki.lol'
  557. ],
  558. resultContainerSelectors: ['div#overflow']
  559. },
  560. 'stract': {
  561. hosts: ['stract.com'],
  562. resultContainerSelectors: ['div.col-start-1.flex']
  563. },
  564. 'etools': {
  565. hosts: ['etools.ch'],
  566. // resultContainerSelectors: ['table.result']
  567. },
  568. 'mojeek': {
  569. hosts: ['mojeek.com'],
  570. resultContainerSelectors: ['div.container.serp-results']
  571. },
  572. 'yep': {
  573. hosts: ['yep.com']
  574. },
  575. 'torry': {
  576. hosts: ['torry.io'],
  577. resultContainerSelectors: ['div.searpListouterappend'],
  578. attribute: 'data-target'
  579. }
  580. // ... more search engines
  581. };
  582.  
  583. // Function to modify URLs and optionally text
  584. const modifyUrls = (engine, observer, resultContainer, engineInfo) => {
  585. try {
  586. const selectors = selectorRules[engine];
  587. if (selectors) {
  588. // Disconnect the observer to prevent recursive triggering
  589. observer.disconnect();
  590.  
  591. // Modify results
  592. selectors.forEach(rule => {
  593. processElements(rule.selector, rule, engineInfo);
  594. });
  595.  
  596. // Reconnect the observer after DOM modifications are done
  597. observer.observe(resultContainer, { childList: true, subtree: true });
  598. }
  599. } catch (error) {
  600. console.error("URL Modification Error: ", error);
  601. }
  602. };
  603.  
  604. // Function to process elements based on selector and rule
  605. const processElements = (selector, rule, engineInfo) => {
  606. const elements = document.querySelectorAll(selector);
  607. const additionalAttribute = engineInfo.attribute; // Get the additional attribute if specified
  608. if (elements.length > 0) {
  609. elements.forEach(element => {
  610. for (let i = 0; i < urlModificationRules.length; i++) {
  611. try {
  612. const urlRule = urlModificationRules[i];
  613. // update attribute 'href'
  614. if (element.href && urlRule.matchRegex.test(element.href)) {
  615. const newHref = element.href.replace(urlRule.matchRegex, urlRule.replaceWith);
  616. element.href = newHref;
  617. if (rule.updateText || rule.updateChildText) {
  618. updateTextContent(element, rule, newHref);
  619. }
  620. break;
  621. }
  622. // update specified attribute that is not 'href'
  623. else if (additionalAttribute && urlRule.matchRegex.test(element.getAttribute(additionalAttribute))) {
  624. const newURL = element.getAttribute(additionalAttribute).replace(urlRule.matchRegex, urlRule.replaceWith);
  625. element.setAttribute(additionalAttribute, newURL);
  626. if (rule.updateText || rule.updateChildText) {
  627. updateTextContent(element, rule, newURL);
  628. }
  629. break;
  630. }
  631. } catch (error) {
  632. console.error("Update Link/Text Error: ", error);
  633. }
  634. }
  635. });
  636. }
  637. };
  638.  
  639. // Function to update text content (displayed url)
  640. const updateTextContent = (element, rule, newUrl) => {
  641. try {
  642. if (rule.multiElementsForUrlDisplay) {
  643. updateDoubleElementContent(element, rule, newUrl);
  644. } else {
  645. // General handling for other search engines
  646. const targetElement = rule.childSelector ? element.querySelector(rule.childSelector) : element;
  647. updateSingleElementText(targetElement, rule, newUrl);
  648. }
  649. } catch (error) {
  650. console.error("Update Displayed URL Error: ", error);
  651. }
  652. };
  653.  
  654. // Function to update text for multi elements (i.e. DuckDuckGo, Brave)
  655. const updateDoubleElementContent = (element, rule, newUrl) => {
  656. // Remove the "https://" protocol if containProtocol is false
  657. newUrl = rule.containProtocol ? newUrl : removeProtocol(newUrl);
  658.  
  659. let formattedUrl = formatMethod1(newUrl, 70, rule.containProtocol); // Assume max length 70 for splitting
  660. let urlParts = formattedUrl.split(' › ');
  661.  
  662. // Correctly select the first and second <span> elements
  663. let spans = element.querySelectorAll(rule.childSelector);
  664.  
  665. if (spans && spans.length >= 2) {
  666. spans.forEach(clearElementContent);
  667. spans[0].textContent = urlParts[0]; // Update the first part
  668. spans[1].textContent = ' › ' + urlParts.slice(1).join(' › '); // Update the second part
  669. } else {
  670. console.error("Script: Expected structure not found for Double Element URL update!");
  671. }
  672. };
  673.  
  674. // Function to update text for a single element
  675. const updateSingleElementText = (targetElement, rule, newUrl) => {
  676. if (!targetElement) {
  677. console.error("Target DOM Element not found for Single-Element Text update!");
  678. return;
  679. }
  680. if (targetElement) {
  681. let formattedUrl = '';
  682. switch (rule.displayMethod) {
  683. case 1:
  684. formattedUrl = formatMethod1(newUrl, rule.maxLength, rule.containProtocol);
  685. break;
  686. case 2:
  687. formattedUrl = newUrl; // Full URL with protocol
  688. break;
  689. case 3:
  690. formattedUrl = decodeURIComponent(removeProtocol(newUrl)); // Full URL without protocol
  691. break;
  692. }
  693. if (rule.updateText) {
  694. updateTextWithoutOverwriteChildNodes(targetElement, formattedUrl);
  695. } else {
  696. targetElement.textContent = formattedUrl;
  697. }
  698. } else {
  699. console.error("Script: Expected element not found for Single Element URL update!");
  700. }
  701. };
  702.  
  703. // Function for Method 1 (Breadcrumb style URLs), leaving 'https://' intact
  704. const formatMethod1 = (url, maxLength, containProtocol) => {
  705. if (!containProtocol) {
  706. url = removeProtocol(url);
  707. }
  708. // Split the URL while keeping 'https://' intact; Replace the second occurrence of 'https://' with 'https', if exists
  709. // Replace the first occurrence of 'https://' with a placeholder
  710. url = url.replace('https://', 'https›');
  711. // Deal with the second 'https://'
  712. let secondHttpsIndex = url.indexOf('https://');
  713. if (secondHttpsIndex !== -1) {
  714. url = url.substring(0, secondHttpsIndex) + 'https/' + url.substring(secondHttpsIndex + 8);
  715. }
  716. // Split the URL with '/'
  717. let parts = url.split('/');
  718. // Restore the first 'https://' in the URL
  719. parts[0] = parts[0].replace('https›', 'https://');
  720.  
  721. // Join the URL parts with ' › ' and check if it exceeds maxLength
  722. let joinedUrl = parts.join(' › ');
  723. if (joinedUrl.length > maxLength) {
  724. // Apply truncation based on maxLength
  725. let truncatedUrl = joinedUrl.slice(0, maxLength - 3); // Reserve space for '...'
  726. truncatedUrl += '...';
  727. joinedUrl = truncatedUrl;
  728. }
  729.  
  730. // Decode the URL to convert encoded characters to their original form
  731. return decodeURIComponent(joinedUrl);
  732. };
  733.  
  734. // Function to update only the text node within an element, leave the child elements, if exist, intact
  735. const updateTextWithoutOverwriteChildNodes = (element, newContent) => {
  736. let foundTextNode = false;
  737. // Iterate through child nodes
  738. for (const node of element.childNodes) {
  739. // Identify and update the first text node
  740. if (node.nodeType === Node.TEXT_NODE) {
  741. node.nodeValue = newContent;
  742. foundTextNode = true;
  743. break; // Stop after updating the first text node
  744. }
  745. }
  746. };
  747.  
  748. // Remove 'https://' from the URL link
  749. const removeProtocol = (url) => {
  750. return url.replace(/^https?:\/\//, '');
  751. };
  752.  
  753. // Function to clear existing content of an element
  754. const clearElementContent = (element) => {
  755. if (element) {
  756. element.textContent = '';
  757. } else {
  758.  
  759. }
  760. };
  761.  
  762. // Improved function to determine the search engine
  763. const getSearchEngineInfo = () => {
  764. try {
  765. const host = window.location.host;
  766. for (const engine in searchEngines) {
  767. if (searchEngines[engine].hosts.some(instanceHost => host.includes(instanceHost))) {
  768. const selectors = searchEngines[engine].resultContainerSelectors || ['body']; // Default to 'body' if not specified
  769. const attribute = searchEngines[engine].attribute; // Get the attribute if specified
  770. return {
  771. engine,
  772. selectors: selectors,
  773. attribute: attribute
  774. };
  775. }
  776. }
  777. } catch (error) {
  778. console.error("Error determining search engine: ", error);
  779. }
  780. };
  781.  
  782. const observeToExecute = (engine, selector, engineInfo) => {
  783. const resultContainers = document.querySelectorAll(selector);
  784. if (resultContainers) {
  785. resultContainers.forEach(resultContainer => {
  786. // Observe changes in each result container
  787. const observer = new MutationObserver(() => modifyUrls(engine, observer, resultContainer, engineInfo));
  788. observer.observe(resultContainer, { childList: true, subtree: true });
  789. modifyUrls(engine, observer, resultContainer, engineInfo);
  790. });
  791. }
  792. };
  793.  
  794. // Run the script for the current search engine
  795. try {
  796. const engineInfo = getSearchEngineInfo();
  797. if (engineInfo) {
  798. engineInfo.selectors.forEach(containerSelector => {
  799. observeToExecute(engineInfo.engine, containerSelector, engineInfo);
  800. });
  801. }
  802. } catch (error) {
  803. console.error("Error executing URL Modifier Script: ", error);
  804. }
  805. })();