-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautiful-doggos.js
162 lines (150 loc) · 5.47 KB
/
beautiful-doggos.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
function GetDoggoPreference() {
return new Promise(function(resolve,reject){
var getting = browser.storage.sync.get("PreferredBreed");
getting.then(function(result) {
var PreferredBreedIndex = result.PreferredBreed
return resolve(PreferredBreedIndex)
})
})
}
function ReturnDoggoURL(result) {
return new Promise(function(resolve,reject){
// if no result is returned, or the preference is all doggos
if ((result == null) || (result == "All doggos")) {
// return all doggos
var url = "https://dog.ceo/api/breeds/image/random"
} else {
// otherwise construct the appropriate string and then return this
var url = "https://dog.ceo/api/breed/" + result + "/images/random"
}
return resolve(url)
})
}
function ReturnDoggo(url) {
// Return promise of fetch
return fetch(url)
}
function FindDoggo(jsonResponse) {
// Log for debugging
// If not OK status then return and log error
if (jsonResponse.status != "success") {
return "Unable to load doggos"
} else { // If OK then try to return message value which will
// be a url of a doggo from the api
var FindDoggoReturn = jsonResponse['message']
return FindDoggoReturn
}
}
function ReplaceImageURL(BaseURL, image) {
ReturnDoggo(BaseURL).then(function(value) {
// Find json of response
var JsonResponse = value.json()
// json is actually promise, so to get value need to use then
JsonResponse.then(function(value) {
var lovelyDoggo = FindDoggo(value)
if (lovelyDoggo != "Unable to load doggos") {
image.src = lovelyDoggo
} else {
console.log(`Unable to query for lovely doggos for image`)
}
})}).catch(function(value) {
console.log(value)
})
}
function ReplaceImages() {
// find all images on page
var imageCollection = document.images
var imageCollectionLength = imageCollection.length
var index = 1
// Iterate through all images
for(let image of imageCollection) {
console.log(`Iterating image ${index}/${imageCollectionLength}`)
GetDoggoPreference().then(ReturnDoggoURL).then(function(url) {
ReplaceImageURL(url, image)})
// Increment index
index++
}
}
function GetContinousDoggoSetting() {
// function to query browser storage for continous doggo setting
return new Promise(function(resolve,reject){
var getting = browser.storage.sync.get("ContinousDoggos");
getting.then(function(result) {
var ContinousDoggosBool = result.ContinousDoggos
console.log(`ContinousDoggos: ${ContinousDoggosBool}`)
return resolve(ContinousDoggosBool)
}).catch(function(error) {
console.log(`Error: ${error}`)
return resolve (false)
})
})
}
function CheckImage(image) {
try {
// convert to string
var imageString = image.src.toString()
// check if already doggo
var isDoggo = imageString.includes("https://images.dog.ceo/breeds/")
// return opposite as we only want to return images that are not doggos
return !isDoggo
} catch {
// if for whatever reason we fail then do not update the image
return false
}
}
GetContinousDoggoSetting().then(function(value) {
var ContinousDoggos = false
if ((value != null) && (value != false)) {
ContinousDoggos = true
}
if (ContinousDoggos != false) {
console.log("continousDoggos set to TRUE")
// First populate the url for doggos then replace all images
ReplaceImages()
// then create a timer so new images are replaced
window.setInterval((function() {
// get htmlcollection
var imageCollection = document.images
for (let image of imageCollection) {
// Check if image should be replaced
var imageCheck = CheckImage(image)
if (imageCheck) {
// Replace src
GetDoggoPreference().then(ReturnDoggoURL).then(function(url) {
ReplaceImageURL(url, image)})
}
}
}),1000)
} else {
// Otherwise do not
console.log("continousDoggos set to FALSE")
}
})
// Add listener for info passed from doggify contextmenu click
browser.runtime.onMessage.addListener(request => {
// Check if valid URL is passed back
var requestURL = request.imageToChangeURL
if (requestURL == null || requestURL.length == 0) {
// If not raise error
console.error("Invalid url passed from background script")
return "Invalid url passed from background script"
} else {
// If proper url then find image based on this
var imageCollection = document.images
var Success = false
for(let image of imageCollection) {
// If match is found
if (image.src == requestURL) {
console.log(`Replaced src of image using url ${requestURL}`)
Success = true
// Replace src and return success
GetDoggoPreference().then(ReturnDoggoURL).then(function(url) {
ReplaceImageURL(url, image)})
break
}
}
if (Success != true) {
console.error("No match found for Doggify target")
}
}
})