Skip to content

Commit

Permalink
refactoring and backup data
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanistHere committed Oct 1, 2020
1 parent 00d611f commit 181d815
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 65 deletions.
15 changes: 2 additions & 13 deletions background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
activateHard,
activateEasy,
executeScript,
resetBadgeText
resetBadgeText,
backupData
} from '../constants/functions.js'

// handle install
Expand Down Expand Up @@ -42,18 +43,6 @@ chrome.runtime.onInstalled.addListener((details) => {
}
})

const backupData = () => {
storageGet(['thisWebsiteWork', 'thisWebsiteWorkEasy', 'stats'], response => {
storageSet({
backupData: {
hard: response.thisWebsiteWork,
easy: response.thisWebsiteWorkEasy,
stats: response.stats
}
})
})
}

// handle tab switch(focus)
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.query({ 'active': true }, (info) => {
Expand Down
12 changes: 12 additions & 0 deletions constants/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ const activateMode = (isHard) =>
const activateHard = activateMode(true)
const activateEasy = activateMode(false)

const backupData = () =>
storageGet(['thisWebsiteWork', 'thisWebsiteWorkEasy', 'stats'], response => {
storageSet({
backupData: {
hard: response.thisWebsiteWork,
easy: response.thisWebsiteWorkEasy,
stats: response.stats
}
})
})

export {
querySelector,
isChecked,
Expand All @@ -73,4 +84,5 @@ export {
activateMode,
activateHard,
activateEasy,
backupData,
}
23 changes: 23 additions & 0 deletions content/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ const detectGrad = (state, statsEnabled, element) => {

return state
}
const additionalChecks = (element, state, statsEnabled, shouldRestoreCont, checkElem) => {
if ((getStyle(element, 'filter') != 'none') ||
(getStyle(element, '-webkit-filter') != 'none')) {
setPropImp(element, "filter", "none")
setPropImp(element, "-webkit-filter", "none")

if (statsEnabled) state = addItemToStats(element, state)
}

if (shouldRestoreCont) state = detectGrad(state, statsEnabled, element)

if (element.shadowRoot)
checkElemWithSibl(element.shadowRoot, checkElem)

return state
}
const isDecentElem = element => {
return ((element.nodeName == 'SCRIPT') ||
(element.nodeName == 'HEAD') ||
(element.nodeName == 'BODY') ||
(element.nodeName == 'HTML') ||
(element.nodeName == 'STYLE')) ? false : true
}

// watch DOM
const checkElemWithSibl = (element, checkElem) => {
Expand Down
21 changes: 5 additions & 16 deletions methods/removeAll.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
var removeFixedElems = (statsEnabled) => {
// state
let state = statsEnabled ? {
windowArea: parseFloat(window.innerHeight * window.innerWidth),
cleanedArea: 0,
numbOfItems: 0,
restored: 0
} : null

// unmutable
const doc = document.documentElement
const body = document.body
const elems = body.getElementsByTagName("*")

// methods
const checkElem = elem => {
if ((elem.nodeName == 'SCRIPT') ||
(elem.nodeName == 'HEAD') ||
(elem.nodeName == 'BODY') ||
(elem.nodeName == 'HTML') ||
(elem.nodeName == 'STYLE'))
return
if (!isDecentElem(elem)) return

const elemPosStyle = getStyle(elem, 'position')
if ((elemPosStyle == 'fixed') ||
Expand All @@ -33,16 +31,7 @@ var removeFixedElems = (statsEnabled) => {
setPropImp(elem, "display", "none")
}

if ((getStyle(elem, 'filter') != 'none') ||
(getStyle(elem, '-webkit-filter') != 'none')) {
setPropImp(elem, "filter", "none")
setPropImp(elem, "-webkit-filter", "none")

if (statsEnabled) state = addCountToStats(state)
}

if (elem.shadowRoot)
checkElemWithSibl(elem.shadowRoot, checkElem)
state = additionalChecks(elem, state, statsEnabled, true, checkElem)
}

// remove
Expand Down
20 changes: 2 additions & 18 deletions methods/removeEasy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ var punishEasy = (statsEnabled, shouldRestoreCont) => {
}
}
const checkElem = element => {
if ((element.nodeName == 'SCRIPT') ||
(element.nodeName == 'HEAD') ||
(element.nodeName == 'BODY') ||
(element.nodeName == 'HTML') ||
(element.nodeName == 'STYLE'))
return
if (!isDecentElem(element)) return

const elemPosStyle = getStyle(element, 'position')
if ((elemPosStyle == 'fixed') ||
Expand All @@ -86,18 +81,7 @@ var punishEasy = (statsEnabled, shouldRestoreCont) => {
semanticCheck(element)
}

if ((getStyle(element, 'filter') != 'none') ||
(getStyle(element, '-webkit-filter') != 'none')) {
setPropImp(element, "filter", "none")
setPropImp(element, "-webkit-filter", "none")

if (statsEnabled) state = addItemToStats(element, state)
}

if (shouldRestoreCont) state = detectGrad(state, statsEnabled, element)

if (element.shadowRoot)
checkElemWithSibl(element.shadowRoot, checkElem)
state = additionalChecks(element, state, statsEnabled, shouldRestoreCont, checkElem)
}
// watch DOM
const prevLoop = () => {
Expand Down
20 changes: 2 additions & 18 deletions methods/removeHard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ var punish = (statsEnabled, shouldRestoreCont) => {

// methods
const checkElem = element => {
if ((element.nodeName == 'SCRIPT') ||
(element.nodeName == 'HEAD') ||
(element.nodeName == 'BODY') ||
(element.nodeName == 'HTML') ||
(element.nodeName == 'STYLE'))
return
if (!isDecentElem(element)) return

const elemPosStyle = getStyle(element, 'position')
if ((elemPosStyle == 'fixed') ||
Expand All @@ -46,18 +41,7 @@ var punish = (statsEnabled, shouldRestoreCont) => {
setTimeout(() => element ? setPropImp(element, "display", "none") : false, 10)
}

if ((getStyle(element, 'filter') != 'none') ||
(getStyle(element, '-webkit-filter') != 'none')) {
setPropImp(element, "filter", "none")
setPropImp(element, "-webkit-filter", "none")

if (statsEnabled) state = addItemToStats(element, state)
}

if (shouldRestoreCont) state = detectGrad(state, statsEnabled, element)

if (element.shadowRoot)
checkElemWithSibl(element.shadowRoot, checkElem)
state = additionalChecks(element, state, statsEnabled, shouldRestoreCont, checkElem)
}

// watch DOM
Expand Down

0 comments on commit 181d815

Please sign in to comment.