Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
annndruha committed Apr 3, 2022
1 parent 2c228ac commit fe06ded
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
2 changes: 1 addition & 1 deletion images/icons/autorenew.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/icons/help.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 11 additions & 3 deletions js/edit-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ function updateIconPreview() {
// Icon itself
let preview_div = document.getElementById("preview")
preview_div.setAttribute('link', link_value)
preview_div.setAttribute('icon-link', (varDefined(iconlink)) ? iconlink : '')
loadIcon('preview')
if (varDefined(iconlink)){
preview_div.setAttribute('icon-link', iconlink)
loadIcon('preview')
}
else if (varDefined(link_value)){
loadIcon('preview')
}
else {
setIcon('preview', 'images/icons/help.svg')
}
}

function createEditPopup(id, placeholder, iconlink) {
Expand Down Expand Up @@ -70,7 +78,7 @@ function createEditPopup(id, placeholder, iconlink) {
let preview_div = document.getElementById("preview")
preview_div.setAttribute('link', placeholder)
preview_div.setAttribute('icon-link', (varDefined(iconlink)) ? iconlink : '')
loadIcon('preview')
updateIconPreview()
}

function saveEdit() {
Expand Down
71 changes: 18 additions & 53 deletions js/load-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ function setIcon(id, data) {
}

function cacheIcon(id, iconBase64){
if(id === '30'){
console.log(iconBase64)
if (id !== 'preview'){
chrome.storage.local.get([id], function (res) {
let storage_value = res[id]
storage_value[0]['cache-icon'] = iconBase64
chrome.storage.local.set({[id]: storage_value}, () => {})
setIcon(id, iconBase64)
})
}
chrome.storage.local.get([id], function (res) {
let storage_value = res[id]
storage_value[0]['cache-icon'] = iconBase64
chrome.storage.local.set({[id]: storage_value}, () => {})
else {
setIcon(id, iconBase64)
})
}
}

function clearIconCache(id) {
Expand Down Expand Up @@ -89,71 +91,34 @@ function loadBestIcon(id, link){
toBase64(id, link2, onLoadCallback)
}




// function waitToLoadFavicon(google_img, fav_link, id, loaded1) {
// let fav_img = new Image()
// fav_img.src = fav_link
// fav_img.onload = () => remakeIcon(google_img, fav_img, id, loaded1, true)
// fav_img.onerror = () => remakeIcon(google_img, fav_img, id, loaded1, false)
// }

let google_err_img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsSAAALEgHS3X78AAACiElEQVQ4EaVTzU8TURCf2tJuS7tQtlRb6UKBIkQwkRRSEzkQgyEc6lkOKgcOph78Y+CgjXjDs2i44FXY9AMTlQRUELZapVlouy3d7kKtb0Zr0MSLTvL2zb75eL838xtTvV6H/xELBptMJojeXLCXyobnyog4YhzXYvmCFi6qVSfaeRdXdrfaU1areV5KykmX06rcvzumjY/1ggkR3Jh+bNf1mr8v1D5bLuvR3qDgFbvbBJYIrE1mCIoCrKxsHuzK+Rzvsi29+6DEbTZz9unijEYI8ObBgXOzlcrx9OAlXyDYKUCzwwrDQx1wVDGg089Dt+gR3mxmhcUnaWeoxwMbm/vzDFzmDEKMMNhquRqduT1KwXiGt0vre6iSeAUHNDE0d26NBtAXY9BACQyjFusKuL2Ry+IPb/Y9ZglwuVscdHaknUChqLF/O4jn3V5dP4mhgRJgwSYm+gV0Oi3XrvYB30yvhGa7BS70eGFHPoTJyQHhMK+F0ZesRVVznvXw5Ixv7/C10moEo6OZXbWvlFAF9FVZDOqEABUMRIkMd8GnLwVWg9/RkJF9sA4oDfYQAuzzjqzwvnaRUFxn/X2ZlmGLXAE7AL52B4xHgqAUqrC1nSNuoJkQtLkdqReszz/9aRvq90NOKdOS1nch8TpL555WDp49f3uAMXhACRjD5j4ykuCtf5PP7Fm1b0DIsl/VHGezzP1KwOiZQobFF9YyjSRYQETRENSlVzI8iK9mWlzckpSSCQHVALmN9Az1euDho9Xo8vKGd2rqooA8yBcrwHgCqYR0kMkWci08t/R+W4ljDCanWTg9TJGwGNaNk3vYZ7VUdeKsYJGFNkfSzjXNrSX20s4/h6kB81/271ghG17l+rPTAAAAAElFTkSuQmCC"


function onLoadCallback(id, src, base64img, err = null){
function onLoadCallback(id, src, base64img){
let imgOld = document.getElementById('icon-' + id)
if (imgOld === null) {return} // When remake grid so fast
// if (varDefined(err)){
// cacheIcon(id, 'images/icons/language.svg')
// }
let img = new Image()
img.onload = function () {
// console.log(this.naturalWidth, src)
if (this.naturalWidth > imgOld.naturalWidth){
if (this.src !== google_err_img){
cacheIcon(id, base64img)
}
else {
cacheIcon(id, 'images/icons/language.svg')
}
}else if (this.src === google_err_img && imgOld.naturalWidth === 25){
cacheIcon(id, 'images/icons/language.svg')
}
// if (varDefined(err) && imgOld.naturalWidth === 67){
// cacheIcon(id, 'images/icons/language.svg')
// }
}
img.src = base64img
}

function toBase64(id, src, callback) {
fetch(src)
.then(function(response){
// if (response.ok){
return response.blob()
// else {
// Promise.reject('is not ok: ' + response.status)
// }
}) //.catch(err => console.log(err))
.then((resp) => resp.blob())
.then(imageBlob => {
let reader = new FileReader()
reader.readAsDataURL(imageBlob)
reader.onloadend = function() {
callback(id, src, reader.result)
}
reader.onerror = function (){
callback(id, src, google_err_img, err)
}
}).catch(err => console.log(err)) //callback(id, src, google_err_img, err)
}

// fetch(link, {
// method:'GET',
// charset: 'UTF-8'
// }).then(function (response) {
// return response.text()
// }).then(function (html) {
// let parser = new DOMParser();
// let doc = parser.parseFromString(html, 'text/html')
// console.log(link, doc.title)
// })
let reader = new FileReader()
reader.readAsDataURL(imageBlob)
reader.onloadend = () => callback(id, src, reader.result)
}).catch(err => {callback(id, src, google_err_img)})
}

0 comments on commit fe06ded

Please sign in to comment.