Page MenuHomePhabricator
Paste P2028

Quick and dirty Phriction template bookmarklet
ActivePublic

Authored by rfreebern on Dec 22 2016, 2:28 PM.
javascript:(function () {
var phabricatorURL = 'http://your.phabricator.url';
var templateSlug = encodeURIComponent('"slug/for/template/"');
var tokenURL = phabricatorURL + '/login/refresh/';
var tokenXhr = new XMLHttpRequest();
tokenXhr.onreadystatechange = function () { xhrHandler(tokenXhr, function (rText) {
var json = rText.substr(9); // Get rid of for(;;);
try {
var response = JSON.parse(json);
} catch (e) {
console.log('Failed to parse JSON: ' + e.message);
console.log(json);
return;
}
if (response.hasOwnProperty('payload') && response.payload.hasOwnProperty('token')) {
var csrf = response.payload.token;
var conduitURL = phabricatorURL + '/api/phriction.info';
var templateXhr = new XMLHttpRequest();
templateXhr.onreadystatechange = function () { xhrHandler(templateXhr, function (json) {
try {
var response = JSON.parse(json);
} catch (e) {
console.log('Failed to parse JSON: ' + e.message);
console.log(json);
return;
}
if (response.hasOwnProperty('result') && response.result.hasOwnProperty('content')) {
var textArea = document.getElementById('document-textarea');
if (textArea === null) {
console.log("Can't find a textarea to paste into.");
return;
}
textArea.value = response.result.content;
}
})};
templateXhr.open('POST', conduitURL, true);
templateXhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
templateXhr.setRequestHeader('X-Phabricator-Csrf', csrf);
templateXhr.send('output=json&params[slug]=' + templateSlug);
} else {
console.log('Token response: ' + rText);
return;
}
})};
tokenXhr.open('GET', tokenURL, true);
tokenXhr.send();
function xhrHandler (xhr, callback) {
if (xhr.readyState < 4 || xhr.status !== 200) {
return;
}
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
})();