forked from Gerrit0/old-github-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold-feed.user.js
210 lines (185 loc) · 8.74 KB
/
old-feed.user.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// ==UserScript==
// @name Old Feed
// @namespace https://gerritbirkeland.com/
// @version 0.17.0-fork-andy0130tw.0
// @updateURL https://raw.githubusercontent.com/andy0130tw/old-github-feed/main/old-feed.user.js
// @downloadURL https://raw.githubusercontent.com/andy0130tw/old-github-feed/main/old-feed.user.js
// @description Restores the Following/For You buttons to let you pick your feed
// @author Gerrit Birkeland
// @match https://github.com/
// @match https://github.com/dashboard
// @icon https://github.com/favicon.ico
// @grant none
// ==/UserScript==
(function () {
"use strict";
const feedContainer = document.querySelector("#dashboard feed-container");
// Apparently if this isn't true, then an SSO popup is being shown, so don't do anything.
if (!feedContainer) return;
// Remove Copilot
const copilot = document.querySelector(".copilotPreview__container");
if (copilot) copilot.remove();
const columnContainer = document.querySelector(".feed-content");
columnContainer.classList.remove("flex-justify-center");
columnContainer.style.maxWidth = "100vw";
const feedColumn = columnContainer.querySelector(".feed-main");
feedColumn.style.maxWidth = "100vw";
if (feedColumn.children.length != 2) {
console.warn("[Old Feed] Page does not have expected structure, please report an issue");
return;
}
const news = document.querySelector("#dashboard .news");
function getDashboardCacheStorageKey(uid) {
return `dashboardCache:${uid ?? '?'}`;
}
function probeDataPropContainingUid() {
const hydroView = followingFeedWrapper.querySelector('[data-repository-hovercards-enabled] > [data-hydro-view]');
if (hydroView) return hydroView.dataset.hydroView;
const hydroClick = followingFeedWrapper.querySelector('[data-hydro-click]');
if (hydroClick) return hydroClick.dataset.hydroClick;
// not found
return null;
}
// !!! prevent reinitialization during navigations
const followingFeedWrapper = document.querySelector('#old-feed-following-feed') ?? document.createElement("div");
if (followingFeedWrapper.parentNode == null) {
// XXX: not sure if this way works on every account
const cacheKey = getDashboardCacheStorageKey(document.querySelector('meta[name="octolytics-actor-id"]')?.content);
followingFeedWrapper.id = 'old-feed-following-feed';
followingFeedWrapper.innerHTML = localStorage.getItem(cacheKey) || "";
news.appendChild(followingFeedWrapper);
}
const existingPicker = document.querySelector('#old-feed-picker');
const picker = existingPicker ?? document.createElement("div");
if (picker.parentNode == null) {
news.insertBefore(picker, feedContainer);
}
picker.innerHTML = `
<div class="mb-3" id="old-feed-picker">
<nav class="overflow-hidden UnderlineNav">
<ul class="UnderlineNav-body">
<li class="d-inline-flex">
<a data-show="following" class="feed-button UnderlineNav-item selected">
<span data-content="Following">Following</span>
</a>
</li>
<li class="d-inline-flex">
<a data-show="forYou" class="feed-button UnderlineNav-item">
<span data-content="For You">For You</span>
</a>
</li>
</ul>
<ul class="UnderlineNav-body" style="display: none !important;">
<li class="d-inline-flex">
<span class="loader">Loading...</span>
</li>
</ul>
</nav>
</div>
`;
// !!!
const loadingIndicator = document.createElement('div') // picker.querySelector(".loader");
// FIXME: which variable is for background color of ".feed-background" depends on theme;
// it is not always "--bgColor-inset"
loadingIndicator.style = `position: absolute;
top: 0; left: 0; right: 0; z-index: 99;
pointer-events: none;
font-size: 24px;
text-align: center;
padding: 16px 0 96px;
transition: opacity 150ms ease-out;
background: linear-gradient(0deg, transparent, var(--bgColor-inset, var(--color-canvas-inset)) 60%);
display: flex;
align-items: center;
justify-content: center;
gap: 16px;`
loadingIndicator.innerHTML = `
<picture>
<img style="width: 48px; display: block" src="https://github.githubassets.com/assets/mona-loading-dimmed-5da225352fd7.gif">
</picture>
<div>Loading...</div>`
news.style.position = 'relative'
news.insertBefore(loadingIndicator, feedContainer)
const tabs = { following: followingFeedWrapper, forYou: feedContainer };
picker.addEventListener("click", event => {
const isChildSpanClicked = event.target.tagName === "SPAN" && event.target.parentNode.classList.contains("feed-button") && event.target.parentNode.tagName === "A"
let target = isChildSpanClicked ? event.target.parentNode : event.target;
if (target.tagName !== "A") return;
Object.entries(tabs).forEach(([name, el]) => {
el.style.display = name === target.dataset.show ? "block" : "none";
});
picker.querySelectorAll(".feed-button").forEach(button => {
button.classList.remove("selected");
});
target.classList.add("selected");
localStorage.setItem("dashboardActiveButton", target.dataset.show);
});
picker.querySelector(`[data-show=${localStorage.getItem("dashboardActiveButton") || "following"}]`).click();
let userHasLoadedMore = false;
fetchDashboard();
// GitHub updates the feed every minute unless the user has loaded more, so we'll do the same.
setInterval(() => {
if (userHasLoadedMore === false) {
fetchDashboard();
}
}, 60000);
async function fetchDashboard() {
// !!!
const getContentArea = () => news.querySelector('.news > *:last-child')
loadingIndicator.style.opacity = 1;
getContentArea().style.opacity = .6;
const r = await fetch(`https://github.com/dashboard-feed?page=1`, { headers: { "X-Requested-With": "XMLHttpRequest" } })
const html = await r.text()
// !!!
getContentArea().style.opacity = 1;
loadingIndicator.style.opacity = 0;
// loadingIndicator.textContent = "";
followingFeedWrapper.innerHTML = html;
followingFeedWrapper.querySelector(".ajax-pagination-btn")?.addEventListener("click", () => {
userHasLoadedMore = true;
});
// !!!
let uid
try {
const data = probeDataPropContainingUid();
if (data != null) uid = JSON.parse(data).payload.user_id;
} catch (e) {
console.warn('[old-github-feed] Failed to extract uid from dashboard feed', e);
}
const cacheKey = getDashboardCacheStorageKey(uid);
// Apply pretty paddings for feeds.
followingFeedWrapper.querySelector(".body .py-4")?.style.setProperty("padding-top", "var(--base-size-4, 4px)", "important");
followingFeedWrapper.querySelectorAll(".body .py-4").forEach((e) => {
e.classList.remove("py-4");
e.classList.add("py-3");
});
// Apply the same foreground color for texts.
followingFeedWrapper.querySelectorAll(".body > div > div > div.color-fg-muted").forEach((e) => {
if (!e.nextElementSibling) {
e.querySelector("div").classList.add("color-fg-default");
}
});
// Apply box for non-boxed items.
followingFeedWrapper.querySelectorAll(".body > .d-flex > .d-flex > div > div[class=color-fg-default]").forEach((e) => {
e.classList.add("Box");
e.classList.add("p-3");
e.classList.add("mt-2");
});
// Apply the same colors for feeds.
followingFeedWrapper.querySelectorAll("div.Box.color-bg-overlay").forEach((e) => {
e.classList.remove("color-bg-overlay");
e.classList.remove("color-shadow-medium");
e.classList.add("feed-item-content");
e.classList.add("border");
e.classList.add("color-border-default");
e.classList.add("color-shadow-small");
e.classList.add("rounded-2");
const markdownBody = e.querySelector("div.color-fg-muted.comment-body.markdown-body");
if (markdownBody) {
markdownBody.classList.remove("color-fg-muted");
}
});
// Saving the edited content for the cache.
localStorage.setItem(cacheKey, followingFeedWrapper.innerHTML);
}
})();