@@ -8,6 +8,7 @@ use axum::{
88 http:: HeaderValue ,
99 response:: IntoResponse ,
1010} ;
11+ use axum_extra:: extract:: Host ;
1112use hyper:: header:: CACHE_CONTROL ;
1213use hyper:: {
1314 HeaderMap , StatusCode ,
@@ -23,6 +24,7 @@ use crate::{github, handlers::Context, utils::AppError};
2324pub async fn gh_range_diff (
2425 Path ( ( owner, repo, basehead) ) : Path < ( String , String , String ) > ,
2526 State ( ctx) : State < Arc < Context > > ,
27+ Host ( host) : Host ,
2628) -> axum:: response:: Result < impl IntoResponse , AppError > {
2729 let Some ( ( oldhead, newhead) ) = basehead. split_once ( ".." ) else {
2830 return Ok ( (
@@ -137,6 +139,9 @@ pub async fn gh_range_diff(
137139 // Create the HTML buffer with a very rough approximation for the capacity
138140 let mut html: String = String :: with_capacity ( 800 + old. files . len ( ) * 100 ) ;
139141
142+ // Compute the bookmarklet for the current host
143+ let bookmarklet = bookmarklet ( & host) ;
144+
140145 // Write HTML header, style, ...
141146 writeln ! (
142147 & mut html,
@@ -170,6 +175,7 @@ pub async fn gh_range_diff(
170175</head>
171176<body>
172177<h3>range-diff of {oldbase}..{oldhead} {newbase}..{newhead}</h3>
178+ <p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, so I can be used on GitHub compare page">range-diff</a></p>
173179"#
174180 ) ?;
175181
@@ -311,3 +317,21 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
311317 Ok ( ( ) )
312318 }
313319}
320+
321+ // Create the javascript bookmarklet based on the host
322+ fn bookmarklet ( host : & str ) -> String {
323+ let protocol = if host. starts_with ( "localhost:" ) {
324+ "http"
325+ } else {
326+ "https"
327+ } ;
328+
329+ format ! (
330+ r"javascript:(() => {{
331+ const githubUrlPattern = /^https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/compare\/([^\/]+[.]{{2}}[^\/]+)$/;
332+ const match = window.location.href.match(githubUrlPattern);
333+ if (!match) {{alert('Invalid GitHub Compare URL format.\nExpected: https://github.com/ORG_NAME/REPO_NAME/compare/BASESHA..HEADSHA'); return;}}
334+ const [, orgName, repoName, basehead] = match; window.location = `{protocol}://{host}/gh-range-diff/${{orgName}}/${{repoName}}/${{basehead}}`;
335+ }})();"
336+ )
337+ }
0 commit comments