-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2204 from ayush-848/master
Added an extension Web Capture
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const browserAPI = typeof browser !== 'undefined' ? browser : chrome; | ||
|
||
browserAPI.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
if (request.action === "downloadText") { | ||
browserAPI.downloads.download({ | ||
url: request.url, | ||
filename: request.filename, | ||
saveAs: true | ||
}, () => { | ||
if (browserAPI.runtime.lastError) { | ||
console.error("Download failed:", browserAPI.runtime.lastError); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(function() { | ||
const browserAPI = typeof browser !== 'undefined' ? browser : chrome; | ||
|
||
function extractText() { | ||
const text = document.body.innerText; | ||
const blob = new Blob([text], {type: 'text/plain'}); | ||
const url = URL.createObjectURL(blob); | ||
|
||
browserAPI.runtime.sendMessage({ | ||
action: "downloadText", | ||
url: url, | ||
filename: 'extracted_text.txt' | ||
}); | ||
} | ||
|
||
extractText(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "Screenshot and Text Extractor", | ||
"version": "1.0", | ||
"description": "Take screenshots and extract text from webpages", | ||
"permissions": [ | ||
"activeTab", | ||
"downloads", | ||
"<all_urls>" | ||
], | ||
"browser_action": { | ||
"default_popup": "popup.html" | ||
}, | ||
"background": { | ||
"scripts": ["background.js"], | ||
"persistent": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Webpage Utility</title> | ||
<style> | ||
body { | ||
width: 200px; | ||
padding: 10px; | ||
} | ||
button { | ||
width: 100%; | ||
margin-bottom: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<button id="screenshot">Take Screenshot</button> | ||
<button id="extractText">Extract Text</button> | ||
<script src="popup.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const browserAPI = typeof browser !== 'undefined' ? browser : chrome; | ||
|
||
document.getElementById('screenshot').addEventListener('click', () => { | ||
browserAPI.tabs.query({active: true, currentWindow: true}, (tabs) => { | ||
const tab = tabs[0]; | ||
if (tab.url.startsWith("chrome://") || tab.url.startsWith("about:")) { | ||
alert("Cannot capture screenshot of browser pages."); | ||
return; | ||
} | ||
browserAPI.tabs.captureVisibleTab(null, {format: 'png'}, (dataUrl) => { | ||
if (browserAPI.runtime.lastError) { | ||
alert("Error capturing screenshot: " + browserAPI.runtime.lastError.message); | ||
return; | ||
} | ||
browserAPI.downloads.download({ | ||
url: dataUrl, | ||
filename: 'screenshot.png', | ||
saveAs: true | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
document.getElementById('extractText').addEventListener('click', () => { | ||
browserAPI.tabs.query({active: true, currentWindow: true}, (tabs) => { | ||
const tab = tabs[0]; | ||
if (tab.url.startsWith("chrome://") || tab.url.startsWith("about:")) { | ||
alert("Cannot extract text from browser pages."); | ||
return; | ||
} | ||
browserAPI.tabs.executeScript( | ||
tab.id, | ||
{ file: 'extractText.js' }, | ||
(results) => { | ||
if (browserAPI.runtime.lastError) { | ||
alert("Error extracting text: " + browserAPI.runtime.lastError.message); | ||
} | ||
} | ||
); | ||
}); | ||
}); |