Skip to content

Commit

Permalink
Merge pull request #2204 from ayush-848/master
Browse files Browse the repository at this point in the history
Added an extension Web Capture
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 7, 2024
2 parents 935d197 + abf0a1b commit 0b8d1c2
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Web Capture/background.js
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);
}
});
}
});
17 changes: 17 additions & 0 deletions Web Capture/extractText.js
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();
})();
18 changes: 18 additions & 0 deletions Web Capture/manifest.json
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
}
}
23 changes: 23 additions & 0 deletions Web Capture/popup.html
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>
41 changes: 41 additions & 0 deletions Web Capture/popup.js
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);
}
}
);
});
});

0 comments on commit 0b8d1c2

Please sign in to comment.