Last active
May 8, 2025 17:17
-
-
Save tewilove/fe203caae9046eb3b45551c7bee1bea4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Legacy QC link fixer | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-02-23 | |
// @description Redirect invalid codeaurora URL to the codelinaro one. | |
// @author tewilove | |
// @match https://source.android.com/docs/security/bulletin/* | |
// @match https://docs.qualcomm.com/product/publicresources/securitybulletin/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function processUrl(url) { | |
// First replacement: domain change | |
let newUrl = url.replace( | |
/https:\/\/source\.codeaurora\.org\/quic\//g, | |
'https://git.codelinaro.org/clo/' | |
); | |
// Second replacement: commit URL format change | |
newUrl = newUrl.replace( | |
/\/commit\/\?id=/g, | |
'/-/commit/' | |
); | |
return newUrl; | |
} | |
function replaceLinks() { | |
// Process all links | |
const elements = document.querySelectorAll('a[href]'); | |
elements.forEach(element => { | |
const oldHref = element.getAttribute('href'); | |
if (oldHref) { | |
const newHref = processUrl(oldHref); | |
if (newHref !== oldHref) { | |
element.setAttribute('href', newHref); | |
// Update link text if it exactly matches the old URL | |
if (element.textContent.trim() === oldHref) { | |
element.textContent = newHref; | |
} | |
} | |
} | |
}); | |
// Process other text nodes that might contain URLs | |
const walker = document.createTreeWalker( | |
document.body, | |
NodeFilter.SHOW_TEXT, | |
null, | |
false | |
); | |
let node; | |
while (node = walker.nextNode()) { | |
if (node.parentNode.nodeName !== 'SCRIPT' && | |
node.parentNode.nodeName !== 'STYLE' && | |
node.parentNode.nodeName !== 'TEXTAREA' && | |
node.textContent.match(/source\.codeaurora\.org\/quic\//)) { | |
node.textContent = processUrl(node.textContent); | |
} | |
} | |
} | |
// Initial run | |
replaceLinks(); | |
// Delayed run for dynamic content | |
setTimeout(replaceLinks, 1000); | |
// MutationObserver for dynamically loaded content | |
const observer = new MutationObserver(function(mutations) { | |
let needsUpdate = false; | |
mutations.forEach(mutation => { | |
if (mutation.addedNodes.length) { | |
needsUpdate = true; | |
} | |
}); | |
if (needsUpdate) { | |
replaceLinks(); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true, | |
characterData: true | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment