-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc07cc7
commit 783afb2
Showing
1 changed file
with
45 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}); |