gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
// Select all anchor elements on the page…
- User
// Select all anchor elements on the page document.querySelectorAll('a').forEach(function(link) { const href = link.href; const prefix = 'http://ouo.io/qs/9I7TYR18?s='; if (href.startsWith(prefix)) { // Extract the part after 's=' const newHref = href.substring(prefix.length); // Update the link's href link.href = newHref; } });
optimize code and refactor to all "ouo.io/*?s="
- Assistant
You want to optimize the code and handle all URLs matching the pattern:
ouo.io/*?s=and update the href to the part after the
s=query parameter, irrespective of the sub-path before?s=.
Refactored Code
document.querySelectorAll('a').forEach(link => { try { const url = new URL(link.href); if (url.hostname === 'ouo.io' && url.searchParams.has('s')) { // Get the 's' param value const sValue = url.searchParams.get('s'); if (sValue) { // Update href to s param value link.href = sValue; } } } catch (e) { // Invalid URL - skip } });
Explanation:
- Uses the
URLobject to parse the href safely. - Checks if hostname is
ouo.io. - Detect any path under
ouo.io/*and look for?s=query param. - Replaces href with the
sparameter value. try/catchhandles invalid URLs gracefully.- Uses arrow functions and cleaner code style.
Let me know if you want it further minified or wrapped as a function!
- Uses the