Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
187 lines (156 loc) · 5.38 KB
/
Copy pathcontent.js
File metadata and controls
187 lines (156 loc) · 5.38 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
// Content script for Solanam Image Processor
// This script can interact with web pages
class SolanamContentScript {
constructor() {
this.init()
}
init() {
// Listen for messages from the extension popup and background
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'processImageFromContext') {
this.processImageFromContext(message.imageUrl)
.then(imageData => sendResponse({ success: true, imageData }))
.catch(error => sendResponse({ success: false, error: error.message }))
return true // Keep the message channel open for async response
} else if (message.action === 'exportImageDirect') {
this.exportImageDirect(message.imageUrl, message.format)
.then(() => sendResponse({ success: true }))
.catch(error => sendResponse({ success: false, error: error.message }))
return true
} else if (message.action === 'extractImages') {
this.extractPageImages()
.then(images => sendResponse({ images }))
.catch(error => sendResponse({ error: error.message }))
return true
}
})
}
async processImageFromContext(imageUrl) {
try {
// Create a temporary image element
const img = new Image()
img.crossOrigin = 'anonymous'
return new Promise((resolve, reject) => {
img.onload = () => {
try {
// Create canvas to convert image
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = img.naturalWidth
canvas.height = img.naturalHeight
ctx.drawImage(img, 0, 0)
// Convert to data URL
const dataUrl = canvas.toDataURL('image/png')
resolve({
src: imageUrl,
dataUrl: dataUrl,
width: img.naturalWidth,
height: img.naturalHeight,
timestamp: Date.now()
})
} catch (error) {
reject(new Error('Failed to process image: ' + error.message))
}
}
img.onerror = () => {
reject(new Error('Failed to load image from URL'))
}
img.src = imageUrl
})
} catch (error) {
throw new Error('Failed to process image: ' + error.message)
}
}
async exportImageDirect(imageUrl, format) {
try {
const img = new Image()
img.crossOrigin = 'anonymous'
return new Promise((resolve, reject) => {
img.onload = () => {
try {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = img.naturalWidth
canvas.height = img.naturalHeight
ctx.drawImage(img, 0, 0)
// Convert to desired format
const mimeType = this.getMimeType(format)
const quality = format === 'jpeg' ? 0.9 : undefined
canvas.toBlob((blob) => {
if (!blob) {
reject(new Error('Failed to create blob'))
return
}
// Create download link
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = `solanam-export-${Date.now()}.${this.getExtension(format)}`
// Trigger download
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
// Cleanup
URL.revokeObjectURL(url)
resolve()
}, mimeType, quality)
} catch (error) {
reject(error)
}
}
img.onerror = () => {
reject(new Error('Failed to load image'))
}
img.src = imageUrl
})
} catch (error) {
throw error
}
}
getMimeType(format) {
const mimeTypes = {
'jpeg': 'image/jpeg',
'png': 'image/png',
'webp': 'image/webp',
'avif': 'image/avif'
}
return mimeTypes[format] || 'image/png'
}
getExtension(format) {
const extensions = {
'jpeg': 'jpg',
'png': 'png',
'webp': 'webp',
'avif': 'avif'
}
return extensions[format] || 'png'
}
async extractPageImages() {
const images = []
const imgElements = document.querySelectorAll('img')
for (const img of imgElements) {
if (img.src && img.naturalWidth > 100 && img.naturalHeight > 100) {
try {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = img.naturalWidth
canvas.height = img.naturalHeight
ctx.drawImage(img, 0, 0)
const dataUrl = canvas.toDataURL('image/png')
images.push({
src: img.src,
alt: img.alt || 'Image',
width: img.naturalWidth,
height: img.naturalHeight,
dataUrl
})
} catch (error) {
console.warn('Could not extract image:', error)
}
}
}
return images
}
}
// Initialize the content script
new SolanamContentScript()