forked from jerone/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithub_Pages_Linker.user.js
More file actions
107 lines (89 loc) · 3.62 KB
/
Copy pathGithub_Pages_Linker.user.js
File metadata and controls
107 lines (89 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// ==UserScript==
// @id Github_Pages_Linker@https://github.com/jerone/UserScripts
// @name Github Pages Linker
// @namespace https://github.com/jerone/UserScripts/
// @description Add a link to Github Pages (gh-pages) when available.
// @author jerone
// @copyright 2014+, jerone (https://github.com/jerone)
// @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @homepage https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
// @downloadURL https://github.com/jerone/UserScripts/raw/master/Github_Pages_Linker/Github_Pages_Linker.user.js
// @updateURL https://github.com/jerone/UserScripts/raw/master/Github_Pages_Linker/Github_Pages_Linker.user.js
// @supportURL https://github.com/jerone/UserScripts/issues
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
// @icon https://github.githubassets.com/pinned-octocat.svg
// @version 1.2.5
// @grant none
// @run-at document-end
// @include https://github.com/*
// ==/UserScript==
(function () {
String.format = function (string) {
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
return string.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] !== "undefined" ? args[number] : match;
});
};
function addLink() {
if (document.getElementById("GithubPagesLinker")) {
return;
}
var meta = document.querySelector('main h1');
if (!meta) {
return;
}
var branchSelector = document.querySelector('#branch-select-menu');
if (!branchSelector) {
return;
}
var branch = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
if (branch) {
createLink(branch);
} else {
const observer = new MutationObserver(function () {
var branch2 = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
if (branch2) {
observer.disconnect();
createLink(branch2);
}
});
observer.observe(branchSelector, { subtree: true, childList: true });
var dropdown = branchSelector.querySelector('ref-selector');
window.setTimeout(function () {
dropdown.dispatchEvent(new CustomEvent('container-mouseover', { bubbles: true }));
}, 100);
}
function createLink(branch2) {
var tree = branch2.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
var url = String.format("{0}//{1}.github.io/{2}/", tree[0], tree[3], tree[4]);
var div = document.createElement("small");
div.id = "GithubPagesLinker";
meta.parentNode.insertBefore(div, meta.nextSibling);
var img = document.createElement("img");
img.setAttribute("src", "https://github.githubassets.com/images/icons/emoji/octocat.png");
img.setAttribute("align", "absmiddle");
img.classList.add("emoji");
img.style.height = "16px";
img.style.width = "16px";
div.appendChild(img);
div.appendChild(document.createTextNode(" "));
var a = document.createElement("a");
a.setAttribute("href", "{https}://pages.github.com");
a.setAttribute("title", "More info about gh-pages...");
a.style.color = "inherit";
a.appendChild(document.createTextNode("Github Pages"));
div.appendChild(a);
div.appendChild(document.createTextNode(": "));
var aa = document.createElement("a");
aa.setAttribute("href", url);
aa.appendChild(document.createTextNode(url));
div.appendChild(aa);
}
}
// Init;
addLink();
// On pjax;
document.addEventListener('pjax:end', addLink);
})();