Skip to content

Commit 53f366c

Browse files
committed
fix(pr-2-jira): execute script on GitHub SPA navigation
Previously, the script only ran on initial page load. When navigating between GitHub PR pages using the browser's SPA (Single Page Application), the URL would change without a full page reload, so the script wouldn't re-detect the page type or initialize features. Added event listeners to detect and respond to URL changes: - popstate: handles back/forward button navigation - pushState override: detects when GitHub navigates without reload - replaceState override: detects when GitHub replaces history states Now the script automatically re-initializes whenever the user navigates between GitHub PRs, eliminating the need to press F5 for the buttons to appear.
1 parent cacb1c6 commit 53f366c

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

scripts/pr-2-jira.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name PR-2-Jira: GitHub & Jira Integration
33
// @namespace https://github.com/scharinger/userscripts
4-
// @version 2.3
4+
// @version 2.4
55
// @description Seamlessly connect GitHub PRs to Jira with smart button placement and automatic link creation
66
// @author Tim Scharinger
77
// @match https://*/*/pull/*
@@ -349,14 +349,42 @@
349349
}
350350
}
351351

352-
// Main initialization
353-
const pageType = detectPageType()
354-
console.log(`${PREFIX} Detected page type: ${pageType}`)
355-
if (pageType === 'github') {
356-
initializeGitHubFeatures()
357-
} else if (pageType === 'jira') {
358-
initializeJiraFeatures()
359-
} else {
360-
console.log(`${PREFIX} No matching page type found`)
352+
// Main initialization function
353+
function initialize() {
354+
const pageType = detectPageType()
355+
console.log(`${PREFIX} Detected page type: ${pageType}`)
356+
if (pageType === 'github') {
357+
initializeGitHubFeatures()
358+
} else if (pageType === 'jira') {
359+
initializeJiraFeatures()
360+
} else {
361+
console.log(`${PREFIX} No matching page type found`)
362+
}
363+
}
364+
365+
// Initialize on first load
366+
initialize()
367+
368+
// Listen for GitHub's SPA navigation (back/forward, popstate)
369+
window.addEventListener('popstate', () => {
370+
console.log(`${PREFIX} URL changed (popstate), re-initializing...`)
371+
initialize()
372+
})
373+
374+
// Listen for pushState changes (when GitHub navigates without reload)
375+
// We need to override history.pushState to detect navigation
376+
const originalPushState = window.history.pushState
377+
window.history.pushState = function (...args) {
378+
originalPushState.apply(window.history, args)
379+
console.log(`${PREFIX} URL changed (pushState), re-initializing...`)
380+
initialize()
381+
}
382+
383+
// Also listen for replaceState
384+
const originalReplaceState = window.history.replaceState
385+
window.history.replaceState = function (...args) {
386+
originalReplaceState.apply(window.history, args)
387+
console.log(`${PREFIX} URL changed (replaceState), re-initializing...`)
388+
initialize()
361389
}
362390
})()

0 commit comments

Comments
 (0)