From 783afb2a947bff6bb5bc5ec68f32f2ceb788adb0 Mon Sep 17 00:00:00 2001 From: milanmilosev Date: Sat, 11 Jan 2020 15:11:58 +0100 Subject: [PATCH] store item to session storage --- content.js | 66 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/content.js b/content.js index 062072f..1937617 100644 --- a/content.js +++ b/content.js @@ -1,34 +1,58 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.message === "FETCH_IMAGE") { - // open gallery - document.querySelector("#viewad-image") && - document.querySelector("#viewad-image").click() - - // settimeout to prevent side effects - setTimeout(() => { - // extract images - const imageContainer = - document.querySelectorAll(".ad-image-wrapper .ad-image img") || - document.querySelectorAll("imagebox-thumbnail img") || false; - - - // create images array - const imageArray = Array.from(imageContainer) || null; - const imageSource = - imageArray.length && - imageArray.map(src => src.getAttribute("src")); + updateBackground = (results) => { + console.log('running update') // send results to background listener chrome.runtime.sendMessage({ message: "SHOW_RESULTS", results: { - imageSource: imageSource + imageSource: results } }); + } + + // check storage + let resultsFromStorage = null; + const adElement = + document.querySelector('.boxedarticle--details--full .align-right') && + document.querySelector('.boxedarticle--details--full .align-right').innerText || + false; + + const adNumber = adElement && adElement.match(/[0-9]+/g) || false + + if(adNumber.length) { + resultsFromStorage = sessionStorage.getItem(adNumber); + } + + if(resultsFromStorage) { + updateBackground(JSON.parse(resultsFromStorage)) + + } else { + document.querySelector("#viewad-image") && + document.querySelector("#viewad-image").click() + + // settimeout to prevent side effects + setTimeout(() => { + // extract images + const imageContainer = + document.querySelectorAll(".ad-image-wrapper .ad-image img") || + document.querySelectorAll("imagebox-thumbnail img") || false; - // close gallery - imageSource ? document.querySelector(".mfp-close").click() : null + + // create images array + const imageArray = Array.from(imageContainer) || null; + const imageSource = + imageArray.length && + imageArray.map(src => src.getAttribute("src")); + + sessionStorage.setItem(adNumber, JSON.stringify(imageSource)); + console.log(imageSource) + updateBackground(imageSource); + // close gallery + imageSource ? document.querySelector(".mfp-close").click() : null - }, 1000) + }, 1000) + } } });