Skip to content

Instantly share code, notes, and snippets.

@oKcerG
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oKcerG/a10313edf6187f43f57e to your computer and use it in GitHub Desktop.
Save oKcerG/a10313edf6187f43f57e to your computer and use it in GitHub Desktop.
GitHub link in Qt.io Documentation userscript
// ==UserScript==
// @name GitHub link in Qt.io Documentation
// @namespace http://grecko.fr/
// @version 0.4.1
// @description Add a link to view the source of a class/component on Qt.io documentation pages.
// @author GrecKo
// @match http://doc.qt.io/*
// @match https://doc.qt.io/*
// @match http://doc-snapshots.qt.io/*
// @match https://doc-snapshots.qt.io/*
// @grant GM_xmlhttpRequest
// @updateURL https://gist.github.com/oKcerG/a10313edf6187f43f57e/raw/qtdoc-github-viewsource.user.js
// ==/UserScript==
var head = document.querySelector('head');
var children = head.childNodes;
for (var i=0, len=children.length; i<len; i++) {
if (children[i].nodeType == Node.COMMENT_NODE) {
comment = children[i];
var filename = comment.textContent.trim();
if (filename.endsWith('.qdoc'))
return;
var excludedRepos=['qt','qt-mobility'];
var searchUrl = 'https://api.github.com/search/code?q=user:qtproject';
for (var i = 0; i < excludedRepos.length; ++i)
searchUrl += '+-repo:qtproject/'+excludedRepos[i];
searchUrl += '+filename:'+filename;
GM_xmlhttpRequest ({
method: 'get',
url: searchUrl,
onload: searchListener
});
return;
}
}
function searchListener (r) {
var response = JSON.parse(r.responseText);
var sourceUrl = response.items[0].html_url;
var sourceLink = document.createElement('a');
sourceLink.setAttribute('href', sourceUrl);
sourceLink.textContent = 'View the source on GitHub';
var branch = sourceLink.pathname.split('/')[4];
//sourceLink.pathname = sourceLink.pathname.replace(branch, 'dev');
var title = document.querySelector('.title');
var parent = title.parentNode;
var subtitle = document.querySelector('.subtitle');
var nextSibling = title.nextSibling;
parent.insertBefore(sourceLink, nextSibling);
parent.insertBefore(document.createElement('br'), nextSibling);
if (sourceUrl.endsWith('.cpp')) {
var headerUrl = sourceUrl.replace('.cpp', '.h');
var headerLink = document.createElement('a');
headerLink.setAttribute('href', headerUrl);
headerLink.textContent = 'View the header on GitHub';
GM_xmlhttpRequest ({
method: 'head',
url: headerUrl,
onload: function (response) {
if (response.status === 200) {
parent.insertBefore(headerLink, nextSibling);
parent.insertBefore(document.createElement('br'), nextSibling);
}
}
});
var privateHeaderUrl = sourceUrl.replace('.cpp', '_p.h');
var privateHeaderLink = document.createElement('a');
privateHeaderLink.setAttribute('href', privateHeaderUrl);
privateHeaderLink.textContent = 'View the private header on GitHub';
GM_xmlhttpRequest ({
method: 'head',
url: privateHeaderUrl,
onload: function (response) {
if (response.status === 200) {
parent.insertBefore(privateHeaderLink, nextSibling);
parent.insertBefore(document.createElement('br'), nextSibling);
nextSibling = privateHeaderLink;
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment