-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-issue-title-in-url.user.js
More file actions
59 lines (49 loc) · 2.19 KB
/
Copy pathgh-issue-title-in-url.user.js
File metadata and controls
59 lines (49 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// ==UserScript==
// @name GH Issue Title in URL
// @namespace http://github.com/softwareengineerprogrammer
// @version 1.2
// @description Automatically add descriptive title query parameter to GitHub issue URLs to make them human-readable/referenceable
// @author softwareengineerprogrammer
// @match https://github.com/*/issues/*
// @match https://github.com/*/*/issues/*
// @grant none
// @updateURL https://softwareengineerprogrammer.github.io/userscripts/gh-issue-title-in-url.user.js
// @downloadURL https://softwareengineerprogrammer.github.io/userscripts/gh-issue-title-in-url.user.js
// ==/UserScript==
(function () {
'use strict';
let attempts = 0
const MAX_ATTEMPTS = 69
function setTitleQueryParam() {
let issueTitleElt = document.querySelector('h1 .markdown-title')
if (issueTitleElt === null) {
attempts++
if (attempts < MAX_ATTEMPTS) {
setTimeout(setTitleQueryParam, 111)
} else {
console.debug('[gh-issue-title-in-url.user.js] Failed to find issue title element after', MAX_ATTEMPTS, 'attempts, giving up =(')
}
return
}
let titleSnippet = issueTitleElt.innerText
const customReplacements = new Map([
[' ', '+'],
['`', ''],
['<', 'lt'],
['>', 'gt'],
['℃', 'C']
])
customReplacements.forEach(function (value, key, map) {
titleSnippet = titleSnippet.replaceAll(key, value)
})
let queryParams = new URLSearchParams(window.location.search)
queryParams.set("title", 'GH_ISSUE_TITLE')
let queryParamsToString = queryParams.toString()
// console.debug('Encoded URL component index in query params.toString()',queryParamsToString.indexOf(encodeURIComponent(titleSnippet)))
// console.debug('queryParams title', queryParams.get('title'))
let adjustedParams = queryParamsToString.replace('GH_ISSUE_TITLE', titleSnippet)
adjustedParams = adjustedParams.replace(/%[0-9][A-F]/g, '')
history.replaceState(null, null, "?" + adjustedParams)
}
setTitleQueryParam()
})();