-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.ts
194 lines (191 loc) · 7.88 KB
/
background.ts
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
async function retitleIfNeeded(tab: chrome.tabs.Tab): Promise<void> {
const tabDomain = new URL(tab.url).hostname;
let doiInUrl: string | null = null;
if (tabDomain === "scholar.google.com" && tab.title.includes("View article")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
document.title = `[Scholar] ${document.querySelector('meta[property="og:title"]').getAttribute("content")}`;
},
})
}
else if (tabDomain === "arxiv.org" && tab.url.includes("/pdf/")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
fetch(tab.url.replace("/pdf/","/abs/"))
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
}
else if (tabDomain === "aclanthology.org" && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
fetch(tab.url.replace(".pdf","/"))
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
const link = (document.querySelector("link[rel*='icon']") as HTMLLinkElement) || document.createElement('link');
(link as HTMLLinkElement).type = 'image/x-icon';
(link as HTMLLinkElement).rel = 'shortcut icon';
(link as HTMLLinkElement).href = 'https://aclanthology.org/aclicon.ico';
document.head.appendChild(link);
}
});
}
else if (tabDomain === "proceedings.mlr.press" && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
const htmlUrl = tab.url.split("/").slice(0,-1).join("/") + ".html";
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
}
else if (tabDomain === "www.jmlr.org" && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
const htmlUrl = tab.url.split("/").slice(0,-1).join("/").replace("/volume", "/v") + ".html";
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
}
else if ((tabDomain === "proceedings.neurips.cc" || tabDomain === "proceedings.nips.cc") && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
const htmlUrl = tab.url.replace("/file/", "/hash/").replace("-Paper", "-Abstract").replace(".pdf", ".html");
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
}
else if (tabDomain === "openreview.net" && tab.url.includes("/pdf?")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
const htmlUrl = tab.url.replace("/pdf?", "/forum?");
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}` }, 1000);
}))
},
args: [tab]
})
}
else if (tabDomain === "openaccess.thecvf.com" && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "") return;
const htmlUrl = tab.url.replace("/papers/", "/html/").replace(".pdf", ".html");
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<meta name="citation_title" content="(.*)">/)[1]}`; }, 1000);
}))
},
args: [tab]
})
}
if (tabDomain === "ieeexplore.ieee.org" && tab.url.includes("/stamp/stamp.jsp")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
if (document.title !== "IEEE Xplore Full-Text PDF:") return;
const docid = tab.url.match(/arnumber=([^&]+)/)[1];
const htmlUrl = `https://ieeexplore.ieee.org/document/${docid}`;
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}`; }, 1000);
}))
},
args: [tab]
})
}
//https://www.biorxiv.org/content/10.1101/2025.01.05.631349v1
//https://www.biorxiv.org/content/10.1101/2025.01.05.631349v1.full.pdf
else if (tabDomain === "www.biorxiv.org" && tab.url.endsWith(".pdf")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab) => {
console.log(document.title);
if (document.title !== "") return;
const htmlUrl = tab.url.replace(".full.pdf", "").replace(".pdf", "")
fetch(htmlUrl)
.then((response) => response.text()
.then((text) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${text.match(/<title>(.*)<\/title>/)[1]}`; }, 1000);
}))
},
args: [tab]
})
}
// Match any tab where the DOI is included in the URL, along with a pathname
// component of either /doi/ or /pdf/. See #2 for discussion on fine-tuning this.
else if ((tab.url.includes("/pdf/") || tab.url.includes("/doi/"))
&& (doiInUrl = tab.url.match(RegExp("/(10\.[^/]+/[^/?#]+)(?:[/?#]|$)"))?.[1])) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (tab, doiInUrl) => {
if (document.title !== "" || document.contentType !== "application/pdf") return;
fetch(`https://dx.doi.org/${doiInUrl}`, {headers: {"accept": "application/json"}})
.then((response) => response.json()
.then((json) => {
// wait a second to make the change stick.
setTimeout(() => { document.title = `[PDF] ${json.title}` }, 1000);
}))
},
args: [tab, doiInUrl]
})
}
}
// Listener: Checks when a new tab is updated
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== "complete" || !tab.url) return;
retitleIfNeeded(tab);
});