-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
125 lines (107 loc) · 3.29 KB
/
Copy pathbackground.js
File metadata and controls
125 lines (107 loc) · 3.29 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
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'saveToPaperless') {
handleSaveToPaperless(request.data)
.then(result => sendResponse(result))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Keep channel open for async response
}
});
async function handleSaveToPaperless(data) {
try {
// Get settings
const settings = await chrome.storage.sync.get(['paperlessUrl', 'paperlessToken']);
if (!settings.paperlessUrl || !settings.paperlessToken) {
throw new Error('Paperless-ngx settings not configured');
}
let pdfBlob;
if (data.isPDF) {
// Download the PDF from the URL
pdfBlob = await downloadPDF(data.url);
} else {
// Convert webpage to PDF
pdfBlob = await convertPageToPDF(data.tabId);
}
// Upload to Paperless
await uploadToPaperless(
pdfBlob,
data.title,
data.tags,
settings.paperlessUrl,
settings.paperlessToken
);
// Show notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon48.png',
title: 'Paperless-ngx Saver',
message: 'Document saved successfully!'
});
return { success: true };
} catch (error) {
console.error('Error saving to Paperless:', error);
return { success: false, error: error.message };
}
}
async function downloadPDF(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download PDF: ${response.statusText}`);
}
return await response.blob();
} catch (error) {
throw new Error(`Failed to download PDF: ${error.message}`);
}
}
async function convertPageToPDF(tabId) {
try {
// Use Chrome's built-in print to PDF functionality
const pdfData = await chrome.tabs.printToPDF(tabId, {
paperWidth: 8.5,
paperHeight: 11,
marginTop: 0.4,
marginBottom: 0.4,
marginLeft: 0.4,
marginRight: 0.4,
printBackground: true,
preferCSSPageSize: false
});
return new Blob([pdfData], { type: 'application/pdf' });
} catch (error) {
throw new Error(`Failed to convert page to PDF: ${error.message}`);
}
}
async function uploadToPaperless(pdfBlob, title, tags, paperlessUrl, token) {
try {
// Create form data
const formData = new FormData();
// Generate filename
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = title
? `${title.substring(0, 50)}.pdf`
: `document-${timestamp}.pdf`;
formData.append('document', pdfBlob, filename);
if (title) {
formData.append('title', title);
}
if (tags && tags.length > 0) {
formData.append('tags', JSON.stringify(tags));
}
// Upload to Paperless
const response = await fetch(`${paperlessUrl}/api/documents/post_document/`, {
method: 'POST',
headers: {
'Authorization': `Token ${token}`,
},
body: formData
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Upload failed: ${response.status} - ${errorText}`);
}
return await response.json();
} catch (error) {
throw new Error(`Failed to upload to Paperless: ${error.message}`);
}
}