Skip to content

Commit 93b7e90

Browse files
authored
fix Github Pages Linker for new UI in 2021 (#159)
* fix Github Pages Linker for new UI in 2021 * make sure it doesn't run on pages without .file-navigation Fixes #158
1 parent 0180351 commit 93b7e90

File tree

1 file changed

+69
-48
lines changed

1 file changed

+69
-48
lines changed

Github_Pages_Linker/Github_Pages_Linker.user.js

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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.3
17+
// @version 1.2.4
1818
// @grant none
1919
// @run-at document-end
2020
// @include https://github.com/*
@@ -28,59 +28,80 @@
2828
return typeof args[number] !== "undefined" ? args[number] : match;
2929
});
3030
};
31+
32+
var DELAY = 800;
3133

32-
function addLink() {
33-
if(document.getElementById("GithubPagesLinker")) {
34-
return;
35-
}
36-
37-
var meta = document.querySelector(".repository-meta");
38-
if (!meta) {
39-
return;
40-
}
41-
42-
var branch = document.querySelector(".js-navigation-open[data-name='gh-pages']");
43-
if (!branch) {
44-
return;
45-
}
46-
47-
var tree = branch.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
48-
var url = String.format("https://{0}.github.io/{1}/", tree[1], tree[2]);
49-
50-
var div = document.createElement("div");
51-
div.id = "GithubPagesLinker";
52-
div.style.margin = "-10px 0px 10px";
53-
meta.parentNode.insertBefore(div, meta.nextSibling);
54-
55-
var img = document.createElement("img");
56-
img.setAttribute("src", "https://assets-cdn.github.com/images/icons/emoji/octocat.png");
57-
img.setAttribute("align", "absmiddle");
58-
img.classList.add("emoji");
59-
img.style.height = "16px";
60-
img.style.width = "16px";
61-
div.appendChild(img);
62-
63-
div.appendChild(document.createTextNode(" "));
64-
65-
var a = document.createElement("a");
66-
a.setAttribute("href", "https://pages.github.com");
67-
a.setAttribute("title", "More info about gh-pages...");
68-
a.style.color = "inherit";
69-
a.appendChild(document.createTextNode("Github Pages"));
70-
div.appendChild(a);
71-
72-
div.appendChild(document.createTextNode(": "));
73-
74-
var aa = document.createElement("a");
75-
aa.setAttribute("href", url);
76-
aa.appendChild(document.createTextNode(url));
77-
div.appendChild(aa);
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);
7899
}
79100

80101
// Init;
81102
addLink();
82103

83104
// On pjax;
84-
document.addEventListener('pjax:end', addLink);
105+
document.addEventListener('pjax:end', addLink);
85106

86107
})();

0 commit comments

Comments
 (0)