DEV Community

gurupetach
gurupetach

Posted on

google chrome pdf extension

Am trying to make a google chrome extension that will handle pdf downloads. I want it to send downloaded pdfs to server. Please help. This is what I have currently done. My manifest.json

{
    "name": "PDF333",
    "version": "1.0.1",
    "description": "PDF auto download",
    "manifest_version": 3,
    "author": "Petach",

    "icons": {
        "16":"icons/16.png",
        "32":"icons/32.png",
        "48":"icons/48.png",
        "128":"icons/128.png"
    },
    "permissions": [ "downloads", "activeTab"],
    "background": {
    "service_worker": "background.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

And finally background.js as below

chrome.downloads.onChanged.addListener(function (downloadDelta) {

    if (downloadDelta.state && downloadDelta.state.current === "complete" && downloadDelta.mime === "application/pdf") {
        chrome.downloads.search({ id: downloadDelta.id }, function (downloadItems) {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://localhost:4000");
            xhr.setRequestHeader("Content-Type", "application/pdf");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        console.log("PDF uploaded successfully!");
                    } else {
                        console.error("Failed to upload PDF.");
                    }
                }
            };
            xhr.send(downloadItems[0].filename);
        });
    }
});
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs