Skip to content

Commit 95346d1

Browse files
gh-issue-title-in-url.user.js v1.1
1 parent c1d51ec commit 95346d1

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name GH Issue Title in URL
33
// @namespace https://github.com/softwareengineerprogrammer
4-
// @version 1.0
4+
// @version 1.1
55
// @description Automatically add descriptive title query parameter to GitHub issue URLs to make them human-readable/referenceable
66
// @author softwareengineerprogrammer
77
// @match https://github.com/*/issues/*
@@ -11,22 +11,49 @@
1111
// @downloadURL https://softwareengineerprogrammer.github.io/userscripts/gh-issue-title-in-url.user.js
1212
// ==/UserScript==
1313

14-
(function() {
14+
(function () {
1515
'use strict';
1616

17-
let titleSnippet = document.querySelector('h1 .markdown-title').innerText.replaceAll(' ', '+').replaceAll('`','')
17+
let attempts = 0
18+
const MAX_ATTEMPTS = 69
1819

19-
console.debug('GH issue', titleSnippet)
20-
// Construct URLSearchParams object instance from current URL querystring.
21-
let queryParams = new URLSearchParams(window.location.search)
20+
function setTitleQueryParam() {
21+
let issueTitleElt = document.querySelector('h1 .markdown-title')
22+
if (issueTitleElt === null) {
23+
attempts++
24+
if (attempts < MAX_ATTEMPTS) {
25+
setTimeout(setTitleQueryParam, 111)
26+
} else {
27+
console.debug('[gh-issue-title-in-url.user.js] Failed to find issue title element after', MAX_ATTEMPTS, 'attempts, giving up =(')
28+
}
29+
return
30+
}
2231

23-
// Set new or modify existing parameter value.
24-
// queryParams.set("title", titleSnippet)
25-
queryParams.set("title", 'GH_ISSUE_TITLE')
26-
let queryParamsToString = queryParams.toString()
27-
// console.debug('Encoded URL component index in query params.toString()',queryParamsToString.indexOf(encodeURIComponent(titleSnippet)))
28-
// console.debug('queryParams title', queryParams.get('title'))
32+
let titleSnippet = issueTitleElt.innerText
2933

30-
// Replace current querystring with the new one.
31-
history.replaceState(null, null, "?"+queryParamsToString.replace('GH_ISSUE_TITLE', titleSnippet))
34+
const customReplacements = new Map([
35+
[' ', '+'],
36+
['`', ''],
37+
['<', 'gt'],
38+
['>', 'lt'],
39+
['℃', 'C']
40+
])
41+
customReplacements.forEach(function (value, key, map) {
42+
titleSnippet = titleSnippet.replaceAll(key, value)
43+
})
44+
45+
46+
let queryParams = new URLSearchParams(window.location.search)
47+
queryParams.set("title", 'GH_ISSUE_TITLE')
48+
let queryParamsToString = queryParams.toString()
49+
50+
// console.debug('Encoded URL component index in query params.toString()',queryParamsToString.indexOf(encodeURIComponent(titleSnippet)))
51+
// console.debug('queryParams title', queryParams.get('title'))
52+
53+
let adjustedParams = queryParamsToString.replace('GH_ISSUE_TITLE', titleSnippet)
54+
adjustedParams = adjustedParams.replace(/%[0-9][A-F]/g, '')
55+
history.replaceState(null, null, "?" + adjustedParams)
56+
}
57+
58+
setTitleQueryParam()
3259
})();

0 commit comments

Comments
 (0)