From c77d140cb00345d219a6a88b3c88d3dec628cc90 Mon Sep 17 00:00:00 2001 From: uncrypt3d <86315452+uncrypt3d@users.noreply.github.com> Date: Thu, 29 Aug 2024 01:46:35 +0300 Subject: [PATCH] 0.1.0 f avif --- background.js | 49 ++++++++++++++++++ content.js | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 19 +++++++ popup.html | 99 ++++++++++++++++++++++++++++++++++++ popup.js | 17 +++++++ 5 files changed, 322 insertions(+) create mode 100644 background.js create mode 100644 content.js create mode 100644 manifest.json create mode 100644 popup.html create mode 100644 popup.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..44ff487 --- /dev/null +++ b/background.js @@ -0,0 +1,49 @@ +// background.js + +function log(message, type = 'info') { + const timestamp = new Date().toISOString(); + switch (type) { + case 'info': + console.log(`%c[INFO - ${timestamp}] ${message}`, 'color: blue'); + break; + case 'warn': + console.warn(`%c[WARN - ${timestamp}] ${message}`, 'color: orange'); + break; + case 'error': + console.error(`%c[ERROR - ${timestamp}] ${message}`, 'color: red'); + break; + default: + console.log(`[LOG - ${timestamp}] ${message}`); + } +} + +chrome.runtime.onInstalled.addListener(() => { + log('Media Converter Extension installed'); +}); + +chrome.runtime.onStartup.addListener(() => { + log('Media Converter Extension started'); +}); + +chrome.runtime.onSuspend.addListener(() => { + log('Media Converter Extension suspended'); +}); + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.action === "fetchImage") { + fetch(request.url) + .then(response => response.blob()) + .then(blob => { + const reader = new FileReader(); + reader.onloadend = () => { + sendResponse({ dataUrl: reader.result }); + }; + reader.readAsDataURL(blob); + }) + .catch(error => { + console.error('Fetch error:', error); + sendResponse({ error: error.message }); + }); + return true; + } +}); diff --git a/content.js b/content.js new file mode 100644 index 0000000..5ff24e1 --- /dev/null +++ b/content.js @@ -0,0 +1,138 @@ +const proxies = [ + "https://corsproxy.io/?url=", + "https://cors-anywhere.herokuapp.com/", + "https://api.allorigins.win/get?url=", + "https://thingproxy.freeboard.io/fetch/" +]; + +let currentProxyIndex = 0; +let observer; + +function startObserving() { + console.log('Starting to observe media changes'); + observer = new MutationObserver(mutations => { + mutations.forEach(mutation => { + mutation.addedNodes.forEach(node => { + if (node.tagName === 'IMG') { + handleImage(node); + } + }); + }); + }); + + observer.observe(document.body, { childList: true, subtree: true }); + console.log('Observer initialized and listening for DOM changes'); +} + +function stopObserving() { + if (observer) { + observer.disconnect(); + console.log('Observer stopped'); + } +} + +function handleImage(img) { + console.log(`New media element detected: IMG`); + console.log(`Processing media element: IMG`); + + if (!img.src.endsWith('.avif')) { + console.log(`Image is not an AVIF format: ${img.src}`); + return; + } + + img.onload = () => { + console.log(`Image loaded: ${img.src}`); + clickAndConvert(img.src); + }; + + img.onerror = () => { + console.error(`Image load failed: ${img.src}`); + }; + + if (img.complete) { + img.onload(); + } else { + img.src = img.src; + } +} + +function clickAndConvert(imageUrl) { + console.log(`Clicking image to trigger full resolution: ${imageUrl}`); + tryFetching(imageUrl); +} + +function tryFetching(url) { + if (currentProxyIndex >= proxies.length) { + console.error('All proxies failed'); + return; + } + + const proxy = proxies[currentProxyIndex]; + const proxiedUrl = proxy + encodeURIComponent(url); + + fetch(proxiedUrl) + .then(response => { + if (!response.ok) { + throw new Error(`Failed to fetch: ${response.statusText}`); + } + return response.blob(); + }) + .then(blob => { + const img = new Image(); + img.onload = () => { + console.log('Image loaded for conversion'); + convertAvifToJpeg(img); + }; + img.onerror = () => { + console.error('Image load failed. Trying next proxy...'); + currentProxyIndex++; + tryFetching(url); + }; + img.src = URL.createObjectURL(blob); + }) + .catch(error => { + console.error('Fetching failed:', error); + currentProxyIndex++; + tryFetching(url); + }); +} + +function convertAvifToJpeg(img) { + console.log('Converting AVIF image to JPEG...'); + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + + canvas.width = img.naturalWidth; + canvas.height = img.naturalHeight; + + try { + ctx.drawImage(img, 0, 0); + const jpegUrl = canvas.toDataURL('image/jpeg'); + console.log('Downloaded JPEG image:', jpegUrl); + + const link = document.createElement('a'); + link.href = jpegUrl; + link.download = `${generateRandomFilename()}.jpg`; + link.click(); + } catch (error) { + console.error('Failed to convert image:', error); + } +} + +function generateRandomFilename(length = 10) { + const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; + let result = ''; + const charactersLength = characters.length; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === 'startObserving') { + startObserving(); + } else if (message.action === 'stopObserving') { + stopObserving(); + } +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..80ed862 --- /dev/null +++ b/manifest.json @@ -0,0 +1,19 @@ +{ + "manifest_version": 3, + "name": "Ylis.jpg", + "version": "0.1.0", + "permissions": ["activeTab", "scripting", "storage"], + "background": { + "service_worker": "background.js" + }, + "content_scripts": [ + { + "matches": [""], + "js": ["content.js"] + } + ], + "action": { + "default_popup": "popup.html" + } + } + \ No newline at end of file diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..4add5fb --- /dev/null +++ b/popup.html @@ -0,0 +1,99 @@ + + + + Ylis2.jpg + + + + +
+

Ylis2.jpg

+

makeylilautagreatagain

+

AVIF-huutikset.jpg

+ + +
+ + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..a02a148 --- /dev/null +++ b/popup.js @@ -0,0 +1,17 @@ +// popup.js +document.addEventListener('DOMContentLoaded', () => { + const backgrounds = [ + 'https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fgetwallpapers.com%2Fwallpaper%2Ffull%2Fa%2F0%2F7%2F656116.jpg&f=1&nofb=1&ipt=6ef5e82ab7086d7cfa01b0e9b0a48716608e24d8e008bb1b333d112468aab63e&ipo=images', + 'https://images.unsplash.com/photo-1648851633100-cd8ae486646e?q=80&w=1664&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D' + ]; + let currentBackgroundIndex = 0; + function changeBackground() { + document.body.style.backgroundImage = `url(${backgrounds[currentBackgroundIndex]})`; + currentBackgroundIndex = (currentBackgroundIndex + 1) % backgrounds.length; + } + changeBackground(); + document.body.addEventListener('mousemove', changeBackground); + document.getElementById('convert').addEventListener('click', () => { + console.log('Convert button clicked'); + }); +});