-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
149 lines (130 loc) · 4.75 KB
/
background.js
File metadata and controls
149 lines (130 loc) · 4.75 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
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
function localStorageRead(key) {
const value = localStorage.getItem(key);
return value === null ? false : value;
}
function localStorageWrite(key, value) {
return localStorage.setItem(key, value);
}
function localStorageDelete(key) {
return localStorage.removeItem(key);
}
function checkIfRepoExistsAsPrivate(githubKey, url) {
const path = new URL(url).pathname;
const pathSplit = path.split("/");
const ownerSlashRepo = `${pathSplit[1]}/${pathSplit[2]}`;
const myHeaders = new Headers();
myHeaders.append("X-GitHub-Api-Version", "2022-11-28");
myHeaders.append("Accept", "application/vnd.github+json");
myHeaders.append("Authorization", `Bearer ${githubKey}`);
const requestOptions = {
method: "GET",
headers: myHeaders,
redirect: "follow",
};
const serviceUrl = `https://api.github.com/repos/${ownerSlashRepo}`;
return fetch(serviceUrl, requestOptions)
.then((response) => {
if (response.status >= 400) {
let errorMsg = "Unknown error from github API";
if (response.status === 401) {
errorMsg = "Github PAT key is invalid!";
localStorageWrite(
"recentError",
"Given key is invalid, check instructions"
);
localStorageDelete("githubKey");
throw Error(errorMsg);
} else if (response.status === 404) {
console.info("Repo does not exist");
return { 404: true };
} else {
throw Error(errorMsg);
}
} else {
return response.json();
}
})
.then((result) => {
if (Object.keys(result).includes("private") && result["private"]) {
return true;
} else if (result["404"]) {
} else {
console.log(`${serviceUrl}\n${JSON.stringify(result)}`);
return false;
}
})
.catch((error) => console.log(error));
}
function currentTabIsAlreadyGithubContainer(cookieStoreId) {
return browser.tabs
.query({
currentWindow: true,
active: true,
})
.then((tabs) =>
browser.tabs
.get(tabs[0].id)
.then((tab) => tab.cookieStoreId === cookieStoreId)
);
}
function changeContainerManually(cookieStoreId, url) {
browser.tabs
.query({
currentWindow: true,
active: true,
})
.then((tabs) => {
browser.tabs.get(tabs[0].id).then((tab) => {
browser.tabs.create({
url: url,
cookieStoreId: cookieStoreId,
index: tab.index + 1,
pinned: tab.pinned,
});
browser.tabs.remove(tabs[0].id);
});
});
}
console.log("background script started");
chrome.webRequest.onCompleted.addListener(
async (details) => {
const githubContainer = JSON.parse(localStorageRead("githubContainer"));
const githubKey = localStorageRead("githubKey");
const cookieStoreId = githubContainer.cookieStoreId;
if (!cookieStoreId) {
console.error("cookieStoreId not found in githubContainer!");
console.error(`githubContainer: ${githubContainer}`);
return;
}
if (!githubContainer) {
console.error("Github container not set!");
return;
} else if (!githubKey) {
console.error("Github key not set!");
return;
}
try {
if (new URL(details.url).hostname === "github.com") {
if (
!(await currentTabIsAlreadyGithubContainer(
cookieStoreId
)) &&
details.statusCode === 404
) {
console.info("404 detected on github.com:", details.url);
if (
await checkIfRepoExistsAsPrivate(githubKey, details.url)
) {
console.info(
`Repo exists but is private, switching to githubContainer: ${cookieStoreId}`
);
changeContainerManually(cookieStoreId, details.url);
}
}
}
} catch (error) {
console.error("Failed to parse URL:", error);
}
},
{ urls: ["*://github.com/*"] }
);