Bing URL Decoder

Decode the Bing URLs to get the direct result page URL

目前为 2023-05-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Bing URL Decoder
  3. // @namespace https://github.com/Alistair1231/my-userscripts/
  4. // @version 0.2.8
  5. // @description Decode the Bing URLs to get the direct result page URL
  6. // @author Alistair1231
  7. // @icon https://icons.duckduckgo.com/ip2/bing.com.ico
  8. // @grant none
  9. // @match https://www.bing.com/*
  10. // @license MIT
  11. // ==/UserScript==
  12. // https://greasyfork.org/en/scripts/464094-bing-url-decoder
  13.  
  14. (function () {
  15. 'use strict';
  16. // https://stackoverflow.com/a/70429872
  17. function unicodeBase64Decode(text){
  18. text = text.replace(/\s+/g, '').replace(/\-/g, '+').replace(/\_/g, '/');
  19. return decodeURIComponent(Array.prototype.map.call(window.atob(text),function(c){return'%'+('00'+c.charCodeAt(0).toString(16)).slice(-2);}).join(''));
  20. }
  21.  
  22. function decodeBingUrl(url) {
  23. const remove_alink = (x) => {
  24. if (!x.startsWith("https://bing.com/alink/")) {
  25. return x;
  26. }
  27. const urlStartIndex = x.indexOf("url=") + 4;
  28. const urlEndIndex = x.indexOf("&", urlStartIndex);
  29. if (urlEndIndex === -1) {
  30. return x.substring(urlStartIndex);
  31. }
  32. return x.substring(urlStartIndex, urlEndIndex);
  33. };
  34.  
  35. // check if has leading "a1" characters, then Remove the leading "a1" characters from the value of the "u" parameter
  36. var uParamValue;
  37. // check if url.searchParams.get('u') is null and skip
  38. if (url.searchParams.get('u') === null) {
  39. return;
  40. }
  41.  
  42. url.searchParams.get('u').startsWith('a1') ? uParamValue = url.searchParams.get('u').substring(2) : uParamValue = url.searchParams.get('u')
  43.  
  44. // Decode the URL-safe Base64-encoded value
  45. // console.log(url.searchParams.get('u'));
  46. // console.log(unicodeBase64Decode(uParamValue));
  47. var base64DecodedValue = unicodeBase64Decode(uParamValue.replace(/_/g, '/').replace(/-/g, '+'));
  48.  
  49. try {
  50. const decodedValue = decodeURIComponent(base64DecodedValue);
  51. return remove_alink(decodedValue);
  52. } catch (error) {
  53. if (error instanceof URIError) {
  54. return remove_alink(base64DecodedValue);
  55. }
  56. }
  57.  
  58. }
  59.  
  60. function decodeBingUrls(links) {
  61. links.forEach(link => {
  62. // test link.href is not undefined
  63. if (link.href === undefined) {
  64. return;
  65. }
  66.  
  67. const decodedUrl = decodeBingUrl(new URL(link.href));
  68. link.href = decodedUrl;
  69. });
  70. }
  71.  
  72. // Decode all "u" parameters in Bing URLs on the page
  73. const links = document.querySelectorAll('a[href*="bing.com"][href*="&u="]');
  74. decodeBingUrls(links);
  75.  
  76. // Use MutationObserver to detect new links added to the page
  77. const observer = new MutationObserver(mutations => {
  78. mutations.forEach(mutation => {
  79. if (mutation.type === 'childList') {
  80. const newLinks = mutation.addedNodes;
  81. decodeBingUrls(newLinks);
  82. }
  83. });
  84. });
  85.  
  86. var mainContent = null;
  87. // if mainContent is null, wait for 1 second and try again
  88. const loadResults = () => {
  89. mainContent = document.getElementById('b_content');
  90. if (mainContent === null) {
  91. setTimeout(loadResults, 1000);
  92. }
  93. }
  94. observer.observe(mainContent, { childList: true, subtree: true });
  95.  
  96. })();