Skip to content

Commit

Permalink
add stats fixes to removeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanistHere committed Sep 15, 2020
1 parent ba4d8f8 commit 119a14d
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions methods/removeAll.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
var removeFixedElems = (statsEnabled) => {
const getStyle = ($elem, property) => window.getComputedStyle($elem, null).getPropertyValue(property)
const setPropImp = ($elem, prop, val) => $elem.style.setProperty(prop, val, "important")
const fixStats = stats => {
let fixedStats = {...stats}
if (isNaN(stats.cleanedArea))
fixedStats = { ...state, cleanedArea: 0 }
if (isNaN(stats.numbOfItems))
fixedStats = { ...state, numbOfItems: 0 }
if (isNaN(stats.restored))
fixedStats = { ...state, restored: 0 }
return fixedStats
}
const setNewData = state =>
chrome.storage.sync.get(['stats'], resp => {
// round to first decimal
const screenValue = Math.round(state.cleanedArea/state.windowArea * 10) / 10
const newStats = {
cleanedArea: resp.stats.cleanedArea + screenValue,
numbOfItems: resp.stats.numbOfItems + state.numbOfItems
let newStats = {
cleanedArea: parseFloat(resp.stats.cleanedArea) + parseFloat(screenValue),
numbOfItems: parseFloat(resp.stats.numbOfItems) + parseFloat(state.numbOfItems),
restored: parseFloat(resp.stats.restored) + parseFloat(state.restored)
}

if (isNaN(newStats.cleanedArea) || isNaN(newStats.numbOfItems))
return
if (isNaN(newStats.cleanedArea) ||
isNaN(newStats.numbOfItems) ||
isNaN(newStats.restored))
newStats = fixStats(newStats)

chrome.storage.sync.set({ stats: newStats })
})
Expand All @@ -20,15 +33,16 @@ var removeFixedElems = (statsEnabled) => {

return isNaN(layoutArea) ? state : {
...state,
numbOfItems: state.numbOfItems + 1,
cleanedArea: state.cleanedArea + layoutArea
numbOfItems: parseFloat(state.numbOfItems) + 1,
cleanedArea: parseFloat(state.cleanedArea) + parseFloat(layoutArea)
}
}

let state = statsEnabled ? {
windowArea: window.innerHeight * window.innerWidth,
windowArea: parseFloat(window.innerHeight * window.innerWidth),
cleanedArea: 0,
numbOfItems: 0
numbOfItems: 0,
restored: 0
} : null

const doc = document.documentElement
Expand Down Expand Up @@ -58,7 +72,7 @@ var removeFixedElems = (statsEnabled) => {
setPropImp(elem, "filter", "none")
setPropImp(elem, "-webkit-filter", "none")

if (statsEnabled) state = { ...state, numbOfItems: state.numbOfItems + 1 }
if (statsEnabled) state = { ...state, numbOfItems: parseFloat(state.numbOfItems) + 1 }
}

if (getStyle(elem, 'position') == 'absolute') {
Expand All @@ -67,7 +81,7 @@ var removeFixedElems = (statsEnabled) => {
if (statsEnabled) state = addItemToStats(elem, state)
}
}

const checkElems = elems => {
const arr = [...elems]
arr.map(checkElem)
Expand Down

0 comments on commit 119a14d

Please sign in to comment.