Skip to content

Commit 88c6a79

Browse files
committed
Bump version to 1.1 and enhance diff button functionality with error handling for URL extraction
amend this to handle PR related links like https://github.com/apache/datafusion/pull/15626/files apache/datafusion#15626 (review)
1 parent c89e079 commit 88c6a79

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

github-pr-diff-button.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name GitHub PR Diff Button
33
// @namespace http://tampermonkey.net/
4-
// @version 1.0
4+
// @version 1.1
55
// @description Adds a button to view PR diff in new tab
66
// @author Siew Kam Onn
77
// @match https://github.com/*/pull/*
@@ -12,6 +12,10 @@
1212
(function () {
1313
"use strict";
1414

15+
/**
16+
* Adds a "View Diff" button next to the Code button in GitHub PR pages
17+
* @returns {void}
18+
*/
1519
function addDiffButton() {
1620
// Find the Code button
1721
const codeButton = document.querySelector(
@@ -31,8 +35,19 @@
3135
diffButton.className = "btn btn-sm diff-view-button ml-2";
3236
diffButton.innerHTML = "View Diff";
3337
diffButton.onclick = () => {
34-
const currentUrl = window.location.href;
35-
GM.openInTab(`${currentUrl}.diff`, true);
38+
try {
39+
const currentUrl = window.location.href;
40+
// Extract the base PR URL (remove hash fragments and extra paths after the PR number)
41+
const match = currentUrl
42+
.split("#")[0]
43+
.match(/https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/);
44+
const basePrUrl = match ? match[0] : currentUrl;
45+
GM.openInTab(`${basePrUrl}.diff`, true);
46+
} catch (error) {
47+
console.error("GitHub PR Diff Button: Error opening diff view", error);
48+
// Fall back to current URL if parsing fails
49+
GM.openInTab(`${window.location.href}.diff`, true);
50+
}
3651
};
3752

3853
// Insert after the details element
@@ -52,4 +67,7 @@
5267

5368
// Run on page load
5469
window.addEventListener("load", addDiffButton);
70+
71+
// Run immediately in case the page is already loaded
72+
addDiffButton();
5573
})();

0 commit comments

Comments
 (0)