-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
160 lines (144 loc) · 5.47 KB
/
Copy pathbackground.js
File metadata and controls
160 lines (144 loc) · 5.47 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
browser.browserAction.onClicked.addListener(async (tab) => {
const url = tab.url;
// Check if the current page is an arXiv page
if (url.includes("arxiv.org")) {
try {
// Execute script in the active tab to get paper information
const result = await browser.tabs.executeScript(tab.id, {
code: `
(function() {
// Function to extract arXiv ID from URL
function extractArXivId(url) {
// Match arXiv ID patterns in abstract or PDF URLs
const abstractMatch = url.match(/arxiv\\.org\\/abs\\/([\\d\\.]+)/);
const pdfMatch = url.match(/arxiv\\.org\\/pdf\\/([\\d\\.]+)/);
return abstractMatch ? abstractMatch[1] : (pdfMatch ? pdfMatch[1] : null);
}
// Get arXiv ID from current URL
const arxivId = extractArXivId(window.location.href);
if (!arxivId) {
return { success: false, message: "Could not find arXiv ID in URL" };
}
// For PDF URLs, we need to construct the abstract URL to get the title
const isPdf = window.location.href.includes('/pdf/');
if (isPdf) {
// Return what we know so far for PDF pages
return {
success: true,
arxivId: arxivId,
abstractUrl: 'https://arxiv.org/abs/' + arxivId,
isPdf: true
};
} else {
// For abstract pages, extract the title directly
const titleElement = document.querySelector("h1.title");
const title = titleElement ? titleElement.textContent.replace('Title:', '').trim() : null;
if (!title) {
return { success: false, message: "Could not find paper title" };
}
return {
success: true,
title: title,
arxivId: arxivId,
abstractUrl: 'https://arxiv.org/abs/' + arxivId,
isPdf: false
};
}
})();
`
});
const paperInfo = result[0];
if (!paperInfo.success) {
browser.notifications.create({
type: "basic",
title: "arXiv Markdown Link Generator",
message: paperInfo.message || "Error generating markdown link"
});
return;
}
// For PDF pages, we need to fetch the abstract page to get the title
if (paperInfo.isPdf) {
try {
// Create a notification that we're fetching the title
browser.notifications.create({
type: "basic",
title: "arXiv Markdown Link Generator",
message: "Fetching paper title from abstract page..."
});
// Execute a background fetch for the abstract page
const titleResult = await browser.tabs.executeScript(tab.id, {
code: `
(function() {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', '${paperInfo.abstractUrl}', true);
xhr.onload = function() {
if (xhr.status === 200) {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(xhr.responseText, 'text/html');
const titleElement = htmlDoc.querySelector("h1.title");
const title = titleElement ? titleElement.textContent.replace('Title:', '').trim() : null;
resolve(title);
} else {
reject('Failed to fetch abstract page');
}
};
xhr.onerror = function() {
reject('Failed to fetch abstract page');
};
xhr.send();
});
})();
`
});
const title = titleResult[0];
if (!title) {
throw new Error("Could not extract paper title from abstract page");
}
paperInfo.title = title;
} catch (error) {
browser.notifications.create({
type: "basic",
title: "arXiv Markdown Link Generator",
message: "Failed to get paper title: " + error.message
});
return;
}
}
// Generate markdown link
const markdownLink = `[${paperInfo.title}](${paperInfo.abstractUrl})`;
// Copy to clipboard
await browser.tabs.executeScript(tab.id, {
code: `
(function() {
const textarea = document.createElement('textarea');
textarea.textContent = \`${markdownLink}\`;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
return true;
})();
`
});
// Show success notification
browser.notifications.create({
type: "basic",
title: "Markdown Link Copied",
message: "The markdown link has been copied to your clipboard!"
});
} catch (error) {
browser.notifications.create({
type: "basic",
title: "Error",
message: "Failed to generate markdown link: " + error.message
});
}
} else {
browser.notifications.create({
type: "basic",
title: "Not an arXiv Page",
message: "This extension only works on arXiv pages"
});
}
});