Skip to content

Instantly share code, notes, and snippets.

@lgarron
Last active July 6, 2017 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lgarron/53b0b26b160204b0092f to your computer and use it in GitHub Desktop.
Save lgarron/53b0b26b160204b0092f to your computer and use it in GitHub Desktop.
Copy a pretty URL version of a Chromium issue (e.g. https://crbug.com/414843).
/*
Convert to a bookmarklet using: https://chriszarate.github.io/bookmarkleter/
When you're on a Chromium issue page, this bookmarklet copies the short URL to clipboard. Also offers a markup version, a snippets version, and the pure ID.
Example: https://crbug.com/445359 instead of https://bugs.chromium.org/p/chromium/issues/detail?id=445359&can=1&q=owner%3Ame%20OR%20reporter%3Ame%20OR%20cc%3Ame%20OR%20commentby%3Ame&sort=-modified&colspec=ID%20Pri%20M%20Status%20Owner%20Summary%20OS%20Modified
*/
// https://github.com/lgarron/clipboard.js
var clipboard = {};
clipboard.copy = (function() {
var _intercept = false;
var _data; // Map from data type (e.g. "text/html") to value.
document.addEventListener("copy", function(e){
if (_intercept) {
_intercept = false;
for (var key in _data) {
e.clipboardData.setData(key, _data[key]);
}
e.preventDefault();
}
});
return function(data) {
return new Promise(function(resolve, reject) {
_intercept = true; // Race condition?
_data = (typeof data === "string" ? {"text/plain": data} : data);
try {
if (document.execCommand("copy")) {
// document.execCommand is synchronous: http://www.w3.org/TR/2015/WD-clipboard-apis-20150421/#integration-with-rich-text-editing-apis
// So we can call resolve() back here.
resolve();
}
else {
_intercept = false;
reject(new Error("Unable to copy. Perhaps it's not available in your browser?"));
}
}
catch (e) {
_intercept = false;
reject(e);
}
});
};
}());
function flash(ms) {
var oldHtmlBackground = document.body.parentElement.style.background;
var oldBodyDisplay = document.body.style.display;
document.body.style.display = "none";
document.body.parentElement.style.background = "#C2DFFF"
setTimeout(function() {
document.body.style.display = oldBodyDisplay;
document.body.parentElement.style.background = oldHtmlBackground;
}, ms);
}
function copyAndFlash(contents) {
clipboard.copy(contents).then(flash.bind(this, 100), function(err) {
console.log(err);
flash(1000)
});
}
function url_param(name, alt) {
var results = RegExp("[\\?&]" + name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]") + "=([^&#<]*)").exec(window.location.href);
if (results == null) return alt;
else return results[1];
};
function getData() {
if (window.location.href.indexOf("bugs.chromium.org/p/chromium/issues") != -1) {
var id = url_param("id");
var summary = document.getElementsByName("summary")[0].value;
var status = document.getElementsByName("status")[0].value;
var hash = location.hash; // Includes the "#" prefix.
if (hash == "#makechanges") {
hash = "";
}
var url = "https://crbug.com/" + id + hash;
return {
id: id,
url: url,
snippet: "[" + id + "](" + url + ") **[" + status + "]** " + summary,
markup: {
"text/plain": "Issue " + id,
"text/html": "<a href=\"" + url + "\">Issue " + id + "</a>"
}
}
} else if (window.location.href.indexOf("https://chromium-review.googlesource.com/c/") != -1) {
var id = document.querySelector(".header-title a[aria-label^=Change]").textContent;
var summary = document.querySelector(".header-title").textContent.split(": ").slice(1).join(": ").trim();
var hash = location.hash; // Includes the "#" prefix.
var url = "https://crrev.com/c/" + id + hash;
return {
id: id,
url: url,
snippet: "[crrev.com/c/" + id + "](" + url + ") " + summary
}
} else if (window.location.href.indexOf("code.google.com/p/chromium/codesearch") != -1) {
return {
url: location.hash.slice(1).split("&")[0].replace(/^chromium\/src\//, "")
}
}
}
try {
copyAndFlash(getData()["markup"]);
} catch (e) {
alert("Something went wrong. You're probably not on a Chromium page.");
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment