Skip to content

Commit 90e5ade

Browse files
committed
Fix opening branch selector for Github Pages Linker. Re-closes #158
1 parent 93b7e90 commit 90e5ade

File tree

1 file changed

+73
-73
lines changed

1 file changed

+73
-73
lines changed

Github_Pages_Linker/Github_Pages_Linker.user.js

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,94 +14,94 @@
1414
// @supportURL https://github.com/jerone/UserScripts/issues
1515
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
1616
// @icon https://github.githubassets.com/pinned-octocat.svg
17-
// @version 1.2.4
17+
// @version 1.2.5
1818
// @grant none
1919
// @run-at document-end
2020
// @include https://github.com/*
2121
// ==/UserScript==
2222

23-
(function() {
23+
(function () {
2424

25-
String.format = function(string) {
25+
String.format = function (string) {
2626
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
27-
return string.replace(/{(\d+)}/g, function(match, number) {
27+
return string.replace(/{(\d+)}/g, function (match, number) {
2828
return typeof args[number] !== "undefined" ? args[number] : match;
2929
});
3030
};
31-
32-
var DELAY = 800;
33-
34-
var triggerEventClick = new MouseEvent('click', {
35-
view: window,
36-
bubbles: true,
37-
cancelable: true
38-
});
39-
40-
function addLink() {
41-
if(document.getElementById("GithubPagesLinker")) {
42-
return;
43-
}
44-
45-
var meta = document.querySelector('.file-navigation');
46-
if (!meta) {
47-
return;
48-
}
49-
50-
var closeDropdown = () => {
51-
document.querySelector('[data-toggle-for="branch-select-menu"]').dispatchEvent(triggerEventClick);
52-
}
53-
54-
var dropdown = document.querySelector('[data-hotkey="w"]');
55-
dropdown.dispatchEvent(triggerEventClick); // open menu to load data
56-
57-
setTimeout(() => {
58-
var branch = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
59-
if (!branch) {
60-
closeDropdown();
61-
return;
62-
}
63-
64-
var tree = branch.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
65-
var url = String.format("{0}//{1}.github.io/{2}/", tree[0], tree[3], tree[4]);
66-
67-
var div = document.createElement("div");
68-
div.id = "GithubPagesLinker";
69-
div.style.margin = "-10px 0px 10px";
70-
meta.parentNode.insertBefore(div, meta.nextSibling);
71-
72-
var img = document.createElement("img");
73-
img.setAttribute("src", "https://github.githubassets.com/images/icons/emoji/octocat.png");
74-
img.setAttribute("align", "absmiddle");
75-
img.classList.add("emoji");
76-
img.style.height = "16px";
77-
img.style.width = "16px";
78-
div.appendChild(img);
79-
80-
div.appendChild(document.createTextNode(" "));
81-
82-
var a = document.createElement("a");
83-
a.setAttribute("href", "{https}://pages.github.com");
84-
a.setAttribute("title", "More info about gh-pages...");
85-
a.style.color = "inherit";
86-
a.appendChild(document.createTextNode("Github Pages"));
87-
div.appendChild(a);
88-
89-
div.appendChild(document.createTextNode(": "));
90-
91-
var aa = document.createElement("a");
92-
aa.setAttribute("href", url);
93-
aa.appendChild(document.createTextNode(url));
94-
div.appendChild(aa);
95-
96-
closeDropdown();
97-
98-
}, DELAY);
31+
32+
function addLink() {
33+
if (document.getElementById("GithubPagesLinker")) {
34+
return;
35+
}
36+
37+
var meta = document.querySelector('main h1');
38+
if (!meta) {
39+
return;
40+
}
41+
42+
var branchSelector = document.querySelector('#branch-select-menu');
43+
if (!branchSelector) {
44+
return;
45+
}
46+
47+
var branch = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
48+
if (branch) {
49+
createLink(branch);
50+
} else {
51+
const observer = new MutationObserver(function () {
52+
var branch2 = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
53+
if (branch2) {
54+
observer.disconnect();
55+
createLink(branch2);
56+
}
57+
});
58+
59+
observer.observe(branchSelector, { subtree: true, childList: true });
60+
61+
var dropdown = branchSelector.querySelector('ref-selector');
62+
window.setTimeout(function () {
63+
dropdown.dispatchEvent(new CustomEvent('container-mouseover', { bubbles: true }));
64+
}, 100);
65+
}
66+
67+
function createLink(branch2) {
68+
var tree = branch2.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
69+
var url = String.format("{0}//{1}.github.io/{2}/", tree[0], tree[3], tree[4]);
70+
71+
var div = document.createElement("small");
72+
div.id = "GithubPagesLinker";
73+
meta.parentNode.insertBefore(div, meta.nextSibling);
74+
75+
var img = document.createElement("img");
76+
img.setAttribute("src", "https://github.githubassets.com/images/icons/emoji/octocat.png");
77+
img.setAttribute("align", "absmiddle");
78+
img.classList.add("emoji");
79+
img.style.height = "16px";
80+
img.style.width = "16px";
81+
div.appendChild(img);
82+
83+
div.appendChild(document.createTextNode(" "));
84+
85+
var a = document.createElement("a");
86+
a.setAttribute("href", "{https}://pages.github.com");
87+
a.setAttribute("title", "More info about gh-pages...");
88+
a.style.color = "inherit";
89+
a.appendChild(document.createTextNode("Github Pages"));
90+
div.appendChild(a);
91+
92+
div.appendChild(document.createTextNode(": "));
93+
94+
var aa = document.createElement("a");
95+
aa.setAttribute("href", url);
96+
aa.appendChild(document.createTextNode(url));
97+
div.appendChild(aa);
98+
}
9999
}
100100

101101
// Init;
102102
addLink();
103103

104104
// On pjax;
105-
document.addEventListener('pjax:end', addLink);
105+
document.addEventListener('pjax:end', addLink);
106106

107107
})();

0 commit comments

Comments
 (0)