Skip to content

Instantly share code, notes, and snippets.

@wxupjack
Created July 11, 2025 17:46
Show Gist options
  • Save wxupjack/2c161b68a072ba93e8066e7987611fdf to your computer and use it in GitHub Desktop.
Save wxupjack/2c161b68a072ba93e8066e7987611fdf to your computer and use it in GitHub Desktop.
Open link in new tab in GitHub [TemperMonkey Script]
// ==UserScript==
// @name Open link in new tab in GitHub
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Ensures all <a> tags on GitHub Pages open in a new tab
// @author wxupjack
// @match *://*.githubpages.com/*
// @match *://*.github.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function updateLinks() {
// Select all <a> tags
const links = document.querySelectorAll('a');
//console.log('all modified <a> tags:', links);
links.forEach(link => {
link.setAttribute('target', '_blank');
});
}
// Run after the page is loaded
if (document.readyState === 'complete' || document.readyState === 'interactive') {
updateLinks();
} else {
window.addEventListener('DOMContentLoaded', updateLinks);
}
// Optional: Also update links if DOM is dynamically changed (for SPAs or AJAX navigation)
const observer = new MutationObserver(updateLinks);
observer.observe(document.body, { childList: true, subtree: true });
})();
// LLM prompt
// Create a Tampermonkey script for me:
// Domain: All GitHub Pages
// Goal: Add `target="_blank"` to all `<a>` tags on the page
// 1. Get the source code after the page loads
// 2. Find all <a> tags
// 3. Add/modify the target to _blank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment