forked from Abhishek2634/Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
285 lines (250 loc) · 8.31 KB
/
Copy pathcontentScript.js
File metadata and controls
285 lines (250 loc) · 8.31 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(() => {
if (globalThis.__SCICOMMONS_CONTENT_SCRIPT_LOADED__) return;
globalThis.__SCICOMMONS_CONTENT_SCRIPT_LOADED__ = true;
const DOI_PATTERN = /10\.\d{4,9}\/[-._;()/:A-Z0-9]+/i;
const cleanText = (value) =>
String(value || "")
.replace(/<[^>]*>/g, " ")
.replace(/\s+/g, " ")
.trim();
const cleanTitle = (value) =>
cleanText(value)
.replace(/^title:\s*/i, "")
.replace(/\s+-\s+arxiv(?::.*)?$/i, "")
.trim();
const unique = (values) => {
const seen = new Set();
return values.filter((value) => {
const key = cleanText(value).toLowerCase();
if (!key || seen.has(key)) return false;
seen.add(key);
return true;
});
};
const safeDecode = (value) => {
try {
return decodeURIComponent(value);
} catch {
return value;
}
};
const textFromMeta = (selectors) => {
for (const selector of selectors) {
const node = document.querySelector(selector);
const value = node?.getAttribute("content")?.trim();
if (value) return value;
}
return "";
};
const allMeta = (selectors) => {
const values = [];
for (const selector of selectors) {
document.querySelectorAll(selector).forEach((node) => {
const value = node.getAttribute("content")?.trim();
if (value) values.push(value);
});
}
return unique(values.map(cleanText));
};
const readJsonLd = () => {
const entries = [];
document.querySelectorAll('script[type="application/ld+json"]').forEach((script) => {
try {
const parsed = JSON.parse(script.textContent || "");
const queue = Array.isArray(parsed) ? [...parsed] : [parsed];
while (queue.length) {
const item = queue.shift();
if (!item || typeof item !== "object") continue;
entries.push(item);
if (Array.isArray(item["@graph"])) queue.push(...item["@graph"]);
if (Array.isArray(item.hasPart)) queue.push(...item.hasPart);
if (Array.isArray(item.mainEntity)) queue.push(...item.mainEntity);
}
} catch {
// Ignore malformed publisher JSON-LD.
}
});
return entries;
};
const jsonLdEntries = readJsonLd();
const typeMatches = (entry, acceptedTypes) => {
const rawType = entry?.["@type"];
const types = Array.isArray(rawType) ? rawType : [rawType];
return types
.filter(Boolean)
.some((type) => acceptedTypes.includes(String(type).toLowerCase()));
};
const scholarlyJsonLd = jsonLdEntries.find((entry) =>
typeMatches(entry, ["scholarlyarticle", "article", "techarticle", "report"])
);
const valueToText = (value) => {
if (!value) return "";
if (typeof value === "string" || typeof value === "number") return cleanText(value);
if (Array.isArray(value)) return value.map(valueToText).filter(Boolean).join(", ");
if (typeof value === "object") {
return cleanText(value.name || value.headline || value.value || value["@id"] || "");
}
return "";
};
const jsonLdField = (keys) => {
if (!scholarlyJsonLd) return "";
for (const key of keys) {
const value = valueToText(scholarlyJsonLd[key]);
if (value) return value;
}
return "";
};
const jsonLdAuthors = () => {
const authors = scholarlyJsonLd?.author || scholarlyJsonLd?.creator;
if (!authors) return [];
const list = Array.isArray(authors) ? authors : [authors];
return unique(list.map(valueToText).filter(Boolean));
};
const jsonLdIdentifiers = () => {
const identifiers = [];
const candidates = [
scholarlyJsonLd?.identifier,
scholarlyJsonLd?.sameAs,
scholarlyJsonLd?.url,
scholarlyJsonLd?.mainEntityOfPage
];
candidates.flat().forEach((candidate) => {
if (!candidate) return;
if (typeof candidate === "object") {
identifiers.push(candidate.value, candidate.name, candidate["@id"], candidate.url);
} else {
identifiers.push(candidate);
}
});
return identifiers.map(valueToText).filter(Boolean);
};
const canonicalUrl = () => {
const href = document.querySelector('link[rel="canonical"]')?.getAttribute("href");
if (!href) return location.href;
try {
return new URL(href, location.href).toString();
} catch {
return location.href;
}
};
const normalizeDoi = (value) => {
const decoded = safeDecode(cleanText(value));
const match = decoded.match(DOI_PATTERN);
const doi = match?.[0] || decoded.replace(/^doi:\s*/i, "").replace(/^https?:\/\/(dx\.)?doi\.org\//i, "");
return doi.replace(/[.,;)\]]+$/, "").trim();
};
const detectDoi = () => {
const metaDoi = textFromMeta([
'meta[name="citation_doi"]',
'meta[property="citation_doi"]',
'meta[name="dc.identifier"]',
'meta[name="DC.Identifier"]',
'meta[name="doi"]',
'meta[name="prism.doi"]',
'meta[property="og:doi"]'
]);
if (metaDoi) return normalizeDoi(metaDoi);
for (const identifier of jsonLdIdentifiers()) {
const normalized = normalizeDoi(identifier);
if (DOI_PATTERN.test(normalized)) return normalized;
}
const candidates = [
...Array.from(document.querySelectorAll("a[href*='doi.org']")).map((a) => a.href),
document.body?.innerText || ""
];
for (const candidate of candidates) {
const match = candidate.match(DOI_PATTERN)?.[0];
if (match) return normalizeDoi(match);
}
return "";
};
const detectPmid = () => {
const metaPmid = textFromMeta([
'meta[name="citation_pmid"]',
'meta[name="ncbi_uid"]',
'meta[name="ncbi_uidbase"]',
'meta[name="pmid"]'
]);
if (metaPmid) return metaPmid.replace(/\D/g, "");
for (const identifier of jsonLdIdentifiers()) {
const match = identifier.match(/\b(?:pmid|pubmed)[:\s/]*(\d{4,})\b/i);
if (match?.[1]) return match[1];
}
const match = location.href.match(/pubmed\.ncbi\.nlm\.nih\.gov\/(\d+)/i);
return match?.[1] || "";
};
const normalizeArxivId = (value) => {
const normalized = cleanText(value)
.replace(/^arxiv:\s*/i, "")
.replace(/^https?:\/\/arxiv\.org\/(?:abs|pdf)\//i, "")
.replace(/\.pdf$/i, "")
.split(/[?#]/)[0]
.trim();
return normalized;
};
const detectArxivId = () => {
const metaArxiv = textFromMeta([
'meta[name="citation_arxiv_id"]',
'meta[name="arxiv_id"]',
'meta[name="eprints.id"]'
]);
if (metaArxiv) return normalizeArxivId(metaArxiv);
for (const identifier of jsonLdIdentifiers()) {
const match = identifier.match(/(?:arxiv:|arxiv\.org\/(?:abs|pdf)\/)([^?#\s]+)/i);
if (match?.[1]) return normalizeArxivId(match[1]);
}
const match = location.href.match(/arxiv\.org\/(?:abs|pdf)\/([^?#]+)/i);
return match?.[1] ? normalizeArxivId(match[1]) : "";
};
const detectTitle = () =>
cleanTitle(
textFromMeta([
'meta[name="citation_title"]',
'meta[property="og:title"]',
'meta[name="dc.title"]',
'meta[name="DC.Title"]',
'meta[name="twitter:title"]'
]) ||
jsonLdField(["headline", "name"]) ||
document.querySelector("h1")?.textContent ||
document.title
);
const detectAbstract = () =>
cleanText(
textFromMeta([
'meta[name="citation_abstract"]',
'meta[name="description"]',
'meta[property="og:description"]',
'meta[name="dc.description"]',
'meta[name="DC.Description"]',
'meta[name="twitter:description"]'
]) ||
jsonLdField(["abstract", "description"]) ||
document.querySelector("#abstract, .abstract, .article-abstract, [class*='abstract']")
?.textContent
);
const detectAuthors = () =>
unique([
...allMeta([
'meta[name="citation_author"]',
'meta[name="dc.creator"]',
'meta[name="DC.Creator"]',
'meta[name="author"]'
]),
...jsonLdAuthors()
]);
const detectPaper = () => ({
doi: detectDoi(),
pmid: detectPmid(),
arxiv_id: detectArxivId(),
title: detectTitle(),
abstract: detectAbstract(),
authors: detectAuthors(),
url: canonicalUrl()
});
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message?.type !== "SCICOMMONS_DETECT_PAPER") return false;
sendResponse({ ok: true, paper: detectPaper() });
return true;
});
})();