|
1 | 1 | // ==UserScript== |
2 | 2 | // @name GH Issue Title in URL |
3 | 3 | // @namespace https://github.com/softwareengineerprogrammer |
4 | | -// @version 1.0 |
| 4 | +// @version 1.1 |
5 | 5 | // @description Automatically add descriptive title query parameter to GitHub issue URLs to make them human-readable/referenceable |
6 | 6 | // @author softwareengineerprogrammer |
7 | 7 | // @match https://github.com/*/issues/* |
|
11 | 11 | // @downloadURL https://softwareengineerprogrammer.github.io/userscripts/gh-issue-title-in-url.user.js |
12 | 12 | // ==/UserScript== |
13 | 13 |
|
14 | | -(function() { |
| 14 | +(function () { |
15 | 15 | 'use strict'; |
16 | 16 |
|
17 | | - let titleSnippet = document.querySelector('h1 .markdown-title').innerText.replaceAll(' ', '+').replaceAll('`','') |
| 17 | + let attempts = 0 |
| 18 | + const MAX_ATTEMPTS = 69 |
18 | 19 |
|
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 | + } |
22 | 31 |
|
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 |
29 | 33 |
|
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() |
32 | 59 | })(); |
0 commit comments