Decode the Bing URLs to get the direct result page URL
当前为
// ==UserScript==
// @name Bing URL Decoder
// @namespace https://github.com/Alistair1231/my-userscripts/
// @version 0.2
// @description Decode the Bing URLs to get the direct result page URL
// @author Alistair1231
// @icon https://icons.duckduckgo.com/ip2/bing.com.ico
// @grant none
// @match https://www.bing.com/*
// @license GPL-3.0
// ==/UserScript==
// https://greasyfork.org/en/scripts/464094-bing-url-decoder
(function () {
'use strict';
// https://stackoverflow.com/a/70429872
function unicodeBase64Decode(text){
text = text.replace(/\s+/g, '').replace(/\-/g, '+').replace(/\_/g, '/');
return decodeURIComponent(Array.prototype.map.call(window.atob(text),function(c){return'%'+('00'+c.charCodeAt(0).toString(16)).slice(-2);}).join(''));
}
function decodeBingUrl(url) {
const remove_alink = (x) => {
if (!x.startsWith("https://bing.com/alink/")) {
return x;
}
const urlStartIndex = x.indexOf("url=") + 4;
const urlEndIndex = x.indexOf("&", urlStartIndex);
if (urlEndIndex === -1) {
return x.substring(urlStartIndex);
}
return x.substring(urlStartIndex, urlEndIndex);
};
// check if has leading "a1" characters, then Remove the leading "a1" characters from the value of the "u" parameter
var uParamValue;
url.searchParams.get('u').startsWith('a1') ? uParamValue = url.searchParams.get('u').substring(2) : uParamValue = url.searchParams.get('u')
// Decode the URL-safe Base64-encoded value
console.log(url.searchParams.get('u'));
console.log(unicodeBase64Decode(uParamValue));
var base64DecodedValue = unicodeBase64Decode(uParamValue.replace(/_/g, '/').replace(/-/g, '+'));
try {
const decodedValue = decodeURIComponent(base64DecodedValue);
return remove_alink(decodedValue);
} catch (error) {
if (error instanceof URIError) {
return remove_alink(base64DecodedValue);
}
}
}
function decodeBingUrls(links) {
links.forEach(link => {
const decodedUrl = decodeBingUrl(new URL(link.href));
link.href = decodedUrl;
});
}
// Decode all "u" parameters in Bing URLs on the page
const links = document.querySelectorAll('a[href*="bing.com"][href*="&u="]');
decodeBingUrls(links);
// Use MutationObserver to detect new links added to the page
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList') {
const newLinks = mutation.addedNodes;
decodeBingUrls(newLinks);
}
});
});
// Observe changes in the main content area of the page
const mainContent = document.querySelector('main');
observer.observe(mainContent, { childList: true, subtree: true });
})();