Skip to content

Instantly share code, notes, and snippets.

@azu
Last active January 16, 2023 05:57
Show Gist options
  • Save azu/65bb779ee095c2a180cdff61aa8f171c to your computer and use it in GitHub Desktop.
Save azu/65bb779ee095c2a180cdff61aa8f171c to your computer and use it in GitHub Desktop.
Greasemonkey: Open GitHub Issue in VSCode
// ==UserScript==
// @name GitHub Issue: Open VSCode
// @namespace info.efcl.open-vscode-
// @match https://github.com/*/*/issues/*
// @match https://github.com/*/*/pull/*
// @grant GM_download
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @version 1.0
// @author azu
// @description 2023/1/16 12:53:36
// ==/UserScript==
const createMarkdownFromBody = (wrapQute = true) => {
const body = document.querySelector(".Layout-main");
// filter p and span tags
const nodes = Array.from(body.querySelectorAll("p, strong"));
// get texts withotut have div tag and error message
const texts = nodes
.filter(node => !node.querySelector("div") && !node.hasAttribute("data-show-on-error"))
.map(node => node.textContent.trim())
.filter(text => text !== "")
if (wrapQute) {
// wrap qute each lines
return texts.map(text => text.split(/\n/).map(line => `> ${line}`).join("\n")).join("\n");
}
return texts.join("\n");
}
const convertBlobToBase64 = blob => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
function download(blob, filename) {
var a = document.createElement("a"),
url = URL.createObjectURL(blob);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
const onClick = async () => {
// const markdownUrl = location.href + ".md";
// parse github url
const match = location.href.match(/https:\/\/github.com\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/(?<type>issues|pull)\/(?<issueNumber>\d+)/);
if (!match) return;
const { owner, repo, type, issueNumber } = match.groups;
// create data url from markdown text
const markdown = createMarkdownFromBody() + "\n\n----\n\n";
// get last line number
const lastLineNumber = markdown.split(/\n/).length;
const markdownBlob = new Blob([markdown], { type: "text/plain" });
const timestamp = Date.now();
const downloadFileName = `${owner}-${repo}-${type}-${issueNumber}--${timestamp}.md`
console.log({
markdown,
owner,
repo,
type,
issueNumber,
downloadFileName
});
download(markdownBlob, downloadFileName);
// wait download
await new Promise(resolve => setTimeout(resolve, 1000));
const fileURL = "/Users/{YOUR_NAME}/Downloads/" + downloadFileName;
console.log("fileURL", fileURL)
const handler = GM_openInTab("vscode://file/" + fileURL + `:${lastLineNumber}:1`)
// close after 1 second
setTimeout(() => {
handler.close();
}, 4000);
};
GM_registerMenuCommand("Open VSCode", onClick)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment