-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
58 lines (47 loc) · 1.75 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "FETCH_IMAGE") {
updateBackground = (results) => {
console.log('running update')
// send results to background listener
chrome.runtime.sendMessage({
message: "SHOW_RESULTS",
results: {
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;
// 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)
}
}
});