-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
79 lines (67 loc) · 2.89 KB
/
Copy pathbackground.js
File metadata and controls
79 lines (67 loc) · 2.89 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
chrome.browserAction.onClicked.addListener(() => {
chrome.tabs.query({ url: "https://github.com/notifications*" }, (tabs) => {
if (tabs.length > 0) {
const tab = tabs[0];
chrome.tabs.update(tab.id, { active: true });
chrome.tabs.executeScript(tab.id, { code: `(${startNotificationRandomizer.toString()})()` });
} else {
chrome.tabs.create({ url: "https://github.com/notifications" }, (newTab) => {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === newTab.id && changeInfo.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
chrome.tabs.executeScript(tabId, { code: `(${startNotificationRandomizer.toString()})()` });
}
});
});
}
});
});
function startNotificationRandomizer() {
function waitForSelector(selector, timeout = 10000) {
return new Promise((resolve, reject) => {
const start = Date.now();
(function check() {
const el = document.querySelector(selector);
if (el) return resolve(el);
if (Date.now() - start > timeout) return reject("Timeout: " + selector);
requestAnimationFrame(check);
})();
});
}
function getRandomElement(arr) {
return arr.length ? arr[Math.floor(Math.random() * arr.length)] : null;
}
function waitForIssueLinks(timeout = 8000, interval = 300) {
return new Promise((resolve, reject) => {
const start = Date.now();
(function check() {
const issueLinks = Array.from(document.querySelectorAll('a.notification-list-item-link'));
if (issueLinks.length > 0) return resolve(issueLinks);
if (Date.now() - start > timeout) return reject("Timeout waiting for issue/PR links");
setTimeout(check, interval);
})();
});
}
async function clickRandomRepoAndIssue() {
try {
console.log("Waiting for repository nav...");
await waitForSelector('nav[aria-label="Repositories"]');
const repoLinks = Array.from(document.querySelectorAll('nav[aria-label="Repositories"] a.ActionListContent'));
console.log("Found repo links:", repoLinks.map(a => a.href));
const visibleRepos = repoLinks.filter(link => link.offsetParent !== null);
const randomRepo = getRandomElement(visibleRepos);
if (!randomRepo) throw new Error("No visible repo links found in nav[aria-label='Repositories']");
console.log("Clicking repo link:", randomRepo.href);
randomRepo.click();
console.log("Waiting for issues/PRs to load...");
const issues = await waitForIssueLinks();
console.log("Issues/PRs found:", issues.length);
const randomIssue = getRandomElement(issues);
console.log("Clicking issue/PR:", randomIssue.href);
randomIssue.click();
} catch (err) {
console.error("Error during notification randomization:", err);
}
}
clickRandomRepoAndIssue();
}