Skip to content

Commit

Permalink
store item to session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmilosev authored and Milan committed Jan 11, 2020
1 parent cc07cc7 commit 783afb2
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions content.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
});

0 comments on commit 783afb2

Please sign in to comment.