Skip to content

Instantly share code, notes, and snippets.

@bluwy
Last active May 23, 2024 09:32
Show Gist options
  • Save bluwy/0ed6a0798cb9e4b94c91484cb4a0b28c to your computer and use it in GitHub Desktop.
Save bluwy/0ed6a0798cb9e4b94c91484cb4a0b28c to your computer and use it in GitHub Desktop.
Userscript to add a delete issue button in the issues page. It opens the issue in a new tab, delete it, and auto-close the tab (sometimes not working). There's also a button to delete all issues in the current page, which opens all the tabs at once. Also works for discussions. BEWARE that deleting issues is not reversible!
// ==UserScript==
// @name GitHub Delete Issues
// @license MIT
// @version 0.1.0
// @description Adds buttons to easily delete issues
// @author bluwy
// @match https://github.com/**
// @grant none
// ==/UserScript==
;(async function () {
'use strict'
if (location.pathname.endsWith('/issues')) {
// Add delete button to each issue row
const issueGroup = document.querySelector('div[aria-label="Issues"]')
const issueRows = issueGroup.querySelectorAll(
'.js-navigation-container > div'
)
for (const issueRow of issueRows) {
const lastCol = issueRow.querySelector('div.flex-shrink-0.col-4')
const span = document.createElement('span')
span.className = 'ml-2 flex-1 flex-shrink-0'
const button = document.createElement('a')
button.className = 'Button Button--secondary Button--medium'
button.innerHTML = 'Delete'
const issueLink = issueRow.querySelector('a').href
button.href = `${issueLink}?github-delete-issues`
button.target = '_blank'
span.appendChild(button)
lastCol.prepend(span)
}
// Add delete all button to table header
const tableHeader = document.querySelector(
'.table-list-filters > div.table-list-header-toggle'
)
const button = document.createElement('button')
button.className = 'Button Button--secondary Button--small'
button.innerHTML = 'Delete all'
button.onclick = () => {
for (const issueRow of issueRows) {
const issueLink = issueRow.querySelector('a').href
window.open(`${issueLink}?github-delete-issues`, '_blank')
window.focus() // cant seem to work
}
}
tableHeader.prepend(button)
} else if (location.pathname.endsWith('/discussions')) {
// Add delete button to each discussion row
const discussionRows = document.querySelectorAll(
'ul[aria-labelledby="discussions-list"] > li'
)
for (const discussionRow of discussionRows) {
const lastCol = discussionRow.querySelector(
'div.pt-2.pl-2.pl-md-0.pr-3.d-flex.flex-shrink-0.flex-items-center'
)
const span = document.createElement('span')
const button = document.createElement('a')
button.className = 'Button Button--secondary Button--medium'
button.innerHTML = 'Delete'
const discussionLink = discussionRow.querySelector('a').href
button.href = `${discussionLink}?github-delete-discussions`
button.target = '_blank'
span.appendChild(button)
lastCol.prepend(span)
}
}
//
else if (location.search === '?github-delete-issues') {
const deleteButton = document.querySelector('details.js-delete-issue')
if (deleteButton) {
deleteButton.click()
}
await wait(100)
// not working
// document.addEventListener('turbo:render', () => {
// // on the next turbo navigation triggered by the confirm button below, close the page
// window.close()
// })
const confirmButton = document.querySelector('button[name="verify_delete"]')
if (confirmButton) {
confirmButton.click()
}
await wait(1000)
window.close()
}
//
else if (location.search === '?github-delete-discussions') {
const deleteButton = document.querySelector('#dialog-show-delete-discussion')
if (deleteButton) {
deleteButton.click()
}
await wait(100)
const confirmButton = document.querySelector('button[data-disable-with="Deleting discussion…"]')
if (confirmButton) {
confirmButton.click()
}
await wait(1000)
window.close()
}
})()
function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment