Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##### content.js
- function getLargestImageURL(img) {
- const srcset = img.getAttribute("srcset");
- if (!srcset) return img.src;
- const parts = srcset.split(",").map(s => s.trim());
- const last = parts[parts.length - 1];
- return last.split(" ")[0];
- }
- function addDownloadButtons() {
- const images = document.querySelectorAll("img");
- images.forEach((img) => {
- const pin = img.closest("[data-test-id='pin']");
- if (!pin) return;
- const footer = pin.querySelector("[data-test-id='pinrep-footer']");
- if (!footer) return;
- if (footer.querySelector(".download-btn")) return;
- const imageURL = getLargestImageURL(img);
- if (!imageURL) return;
- const button = document.createElement("button");
- button.innerText = "Download";
- button.className = "download-btn";
- button.style.position = "absolute";
- button.style.left = "5px";
- button.style.top = "50%";
- button.style.transform = "translateY(-50%)";
- button.style.zIndex = "9999";
- button.onclick = (e) => {
- e.stopPropagation();
- chrome.runtime.sendMessage({
- action: "download",
- url: imageURL
- });
- };
- footer.style.position = "relative";
- footer.appendChild(button);
- });
- }
- const observer = new MutationObserver(addDownloadButtons);
- observer.observe(document.body, { childList: true, subtree: true });
- addDownloadButtons();
- ##### background.js
- chrome.runtime.onMessage.addListener((message) => {
- if (message.action === "download" && message.url) {
- chrome.downloads.download({
- url: message.url,
- filename: "pinterest_" + Date.now() + ".jpg"
- });
- }
- });
- ##### manifest.json
- {
- "manifest_version": 3,
- "name": "Pinterest Auto Downloader",
- "version": "1.0.0",
- "description": "Automatically download images from Pinterest",
- "permissions": [
- "downloads",
- "activeTab",
- "scripting"
- ],
- "host_permissions": [
- "*://*.pinterest.com/*",
- "*://*.pinimg.com/*"
- ],
- "background": {
- "service_worker": "background.js"
- },
- "content_scripts": [
- {
- "matches": ["*://*.pinterest.com/*"],
- "js": ["content.js"],
- "run_at": "document_idle"
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment