Skip to content

Commit

Permalink
Release 0.5.0 and refactor codes
Browse files Browse the repository at this point in the history
  • Loading branch information
evokelektrique committed Oct 15, 2021
1 parent 7a890f6 commit f6c9b7e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 36 deletions.
45 changes: 25 additions & 20 deletions src/resources/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,39 @@ browser.runtime.onConnect.addListener(port => {

// Listener
browser.runtime.onMessage.addListener(async message => {
let result

switch(message.type) {
case 'get_selectors':
case constants.get_selectors:
return await new SelectorDatabase().get_selectors(message.domain)
break

case constants.hide_element:
console.log('hide element')
break

case constants.toggle_status:
console.log('toggle status')
new SettingsDatabase().open().then(async db => {
// Find status from settings database
const key = "status"
const get = await db.settings.get({ key: key })
const status = get.value
const new_status = !status
await db.settings.update(get.id, { value: new_status })

// Toggle blocker
if(new_status) {
refused.start()
} else {
refused.stop()
}

return Promise.resolve({ status: new_status })
})
// Get status
result = await Helper.get_extension_status()

// Update status
const db = await new SettingsDatabase().open()
await db.settings.update(result.id, { value: !result.status })

// Toggle blocker
if(!result.status) {
refused.start()
} else {
refused.stop()
}

return Promise.resolve({ status: !result.status })
break

case constants.get_status:
// Get status
result = await Helper.get_extension_status()

return Promise.resolve(result)
break

default:
Expand Down
4 changes: 3 additions & 1 deletion src/resources/scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ exports.constants = {

// Messages
toggle_status: "TOGGLE_STATUS",
hide_element: "HIDE_ELEMENT"
hide_element: "HIDE_ELEMENT",
get_selectors: "GET_SELECTORS",
get_status: "GET_STATUS"
}
37 changes: 36 additions & 1 deletion src/resources/scripts/helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { SettingsDatabase } from "./databases/settings"

const browser = require("webextension-polyfill")

/**
* Base helpers class
*/
export class Helper {

/**
* Read lines from given texts
*
* @param {steing} body String body to parse
* @return {array} List of parsed lines
*/
static parse_txt(body) {
const lines = []
const splited = body.split("\r\n").map(line => line.split("\n"))
Expand Down Expand Up @@ -68,8 +79,32 @@ export class Helper {
return new Promise(resolve => setTimeout(resolve, ms));
}

/**
* Fetch current tab
*
* @return {object} Current tab information
*/
static async get_current_tab() {
return await browser.tabs.query({currentWindow: true, active: true}).then(tab => tab[0])
return await browser.tabs.query({
currentWindow: true, active: true
}).then(tab => tab[0])
}

/**
* Find status from settings database
*
* @return {object} current extension status and its row ID
*/
static async get_extension_status() {
// Get status
const db = await new SettingsDatabase().open()
const key = "status"
const get = await db.settings.get({ key: key })
const status = get.value

return Promise.resolve({
status: status, id: get.id
})
}

}
23 changes: 20 additions & 3 deletions src/resources/scripts/refused.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ export class Refused {
*/
static upgrade_listener(details) {
// On install
if(details.reason === "install") {}
if(details.reason === "install") {
browser.tabs.create({ url: constants.base_url + "/install.html" })
}

// On update
if(details.reason === "update") {
browser.tabs.create({ url: constants.base_url + "/update.html" })
}
}

/**
* Handle browser alarms
*
* @param {object} alarm AlarmInfo
*/
static async handle_alarms(alarm) {
const db = await new SettingsDatabase().open()

Expand Down Expand Up @@ -98,7 +105,12 @@ export class Refused {
}
}

// Update filters database
/**
* Update filters database
*
* @param {object} db Dexie database instance
* @return {object} Update status and information about time
*/
static async check_filters_update(db) {
let update_status = false

Expand Down Expand Up @@ -133,7 +145,12 @@ export class Refused {
})
}

// Update selectors database
/**
* Update selectors database
*
* @param {object} db Dexie database instance
* @return {object} Update status and information about time
*/
static async check_selectors_update(db) {
let update_status = false

Expand Down
56 changes: 45 additions & 11 deletions src/resources/scripts/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,39 @@ const RefusedWatcher = {
const current_url = window.location.href
const parsed_url = Helper.parse_url(current_url)
if(parsed_url.status) {
return await browser.runtime.sendMessage({ type: 'get_selectors', domain: parsed_url.domain })
return await browser.runtime.sendMessage({
type: constants.get_selectors,
domain: parsed_url.domain
})
}
},

/**
* Get extension status
*
* @return {object} Status
*/
async get_status() {
return await browser.runtime.sendMessage({ type: constants.get_status })
},

/**
* Hide elements on current page
*
* @param {array} selectors List of css selectors
* @return {void}
*/
hide_elements(selectors) {
if(!selectors) {
return;
}

selectors.forEach(selector => {
const elements = document.querySelectorAll(selector.path)
Array.from(elements).forEach(element => {
element.style.display = 'none'
})
})
}

}
Expand All @@ -70,17 +101,20 @@ const RefusedWatcher = {
browser.runtime.onMessage.addListener(message => {
if(message.type === constants.hide_element) {
const data = {}
browser.runtime.sendMessage({ type: constants.hide_element, data: data })
browser.runtime.sendMessage({
type: constants.hide_element,
data: data
})
}

return false;
})
return false
});

RefusedWatcher.get_selectors().then(selectors => {
selectors.forEach(selector => {
const elements = document.querySelectorAll(selector.path)
Array.from(elements).forEach(element => {
element.style.display = 'none'
})
})
RefusedWatcher.get_status().then(async response => {
const status = response.status

if(status) {
const selectors = await RefusedWatcher.get_selectors()
RefusedWatcher.hide_elements(selectors)
}
})

0 comments on commit f6c9b7e

Please sign in to comment.