Skip to content

Commit

Permalink
Refactor (#102)
Browse files Browse the repository at this point in the history
* LOCATIONS Added  code

* LOCATIONS Updated Express Static File Routing

* LOCATIONS Cleanup

* LOCATIONS Updated Readme

* LOCATIONS Cleanup

* LOCATIONS Cleanup

* LOCATIONS Cleanup

* REFACTOR Cleanup

* REFACTOR REFACTOR Updated Settings.js

(cherry Picked From Commit 304f0ee)

* REFACTOR Fixed Settings.js

* REFACTOR Cleanup

* REFACTOR Cleanup

* REFACTOR Cleanup

* REFACTOR Cleanup

* REFACTOR Added Tests For Locations

* REFACTOR Cleanup

* REFACTOR Cleanup

* REFACTOR Cleanup

* REFACTOR Added Tests

Co-authored-by: Hangjit Rai <rai.hangjit@gmail.com>
  • Loading branch information
hrai and Hangjit Rai committed Apr 27, 2021
1 parent a5ea256 commit 387ee1f
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 243 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"start:win32": "yarn build && web-ext run --firefox=\"c:/Program Files/Firefox Developer Edition/firefox.exe\" --start-url localhost:3000",
"start:darwin:linux": "yarn build && web-ext run --firefox=firefox-developer --start-url localhost:3000",
"start:chrome": "yarn build && cd distribution && web-ext run -t chromium --start-url localhost:3000",
"build": "yarn webpack --mode=development",
"build": "yarn webpack",
"build-dev": "yarn webpack --mode=development",
"watch": "yarn build --watch",
"watch-dev": "yarn build-dev --watch",
"web": "nodemon app.js",
"test": "yarn jest",
"test:watch": "yarn jest --watch test/",
Expand Down
47 changes: 18 additions & 29 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ browser.storage.local
])
.then(processResponse, utils.onError);

// const setterDict = {
// [shouldCapitaliseI]: utils.setShouldCapitaliseI,
// [shouldCapitaliseNames]: utils.setShouldCapitaliseNames,
// [shouldCapitaliseAbbreviations]: utils.setShouldCapitaliseAbbreviations,
// [shouldCapitaliseLocations]: utils.setShouldCapitaliseLocations,
// };

let toggleOptionsValue = (changes, variableName) => {
if (changes[variableName] != null) {
const newValue = changes[variableName].newValue;

if (newValue != null) {
utils.setShouldCapitaliseOption(variableName, newValue);
// setterDict[variableName](newValue);
}
}
};

/* Updating the value of this local storage variable in settings.js happens AFTER content.js.
* The browser doesn't register the change and doesn't capitalise I by default after installing the extension.
* This block will capture the event and update the value of 'shouldCapitaliseI'.
Expand All @@ -59,10 +41,10 @@ browser.storage.onChanged.addListener(function (
areaName // string
) {
if (areaName === 'local') {
toggleOptionsValue(changes, shouldCapitaliseI);
toggleOptionsValue(changes, shouldCapitaliseNames);
toggleOptionsValue(changes, shouldCapitaliseAbbreviations);
toggleOptionsValue(changes, shouldCapitaliseLocations);
utils.toggleOptionsValue(changes, shouldCapitaliseI);
utils.toggleOptionsValue(changes, shouldCapitaliseNames);
utils.toggleOptionsValue(changes, shouldCapitaliseAbbreviations);
utils.toggleOptionsValue(changes, shouldCapitaliseLocations);

if (changes.wordsToExclude != null) {
const newValue = changes.wordsToExclude.newValue;
Expand Down Expand Up @@ -102,9 +84,7 @@ function observeInputTags() {
});
}

function processResponse(item) {
sitesToExclude = item.sitesToIgnore;

function setOptions(item) {
utils.setShouldCapitaliseOption(shouldCapitaliseI, item.shouldCapitaliseI);
utils.setShouldCapitaliseOption(
shouldCapitaliseNames,
Expand All @@ -118,12 +98,21 @@ function processResponse(item) {
shouldCapitaliseLocations,
item.shouldCapitaliseLocations
);
}

utils.setConstantsKeyVal(item.constantsKeyVal);
utils.setNamesKeyVal(item.namesKeyVal);
utils.setAbbreviationsKeyVal(item.abbreviationsKeyVal);
utils.setLocationsKeyVal(item.locationsKeyVal);
function setKeyValues(item) {
utils.setKeyValue(constantsKeyVal, item.constantsKeyVal);
utils.setKeyValue(namesKeyVal, item.namesKeyVal);
utils.setKeyValue(abbreviationsKeyVal, item.abbreviationsKeyVal);
utils.setKeyValue(locationsKeyVal, item.locationsKeyVal);
utils.setWordsToExclude(item.wordsToExclude);
}

function processResponse(item) {
sitesToExclude = item.sitesToIgnore;

setOptions(item);
setKeyValues(item);

if (item && sitesToExclude) {
//https://stackoverflow.com/questions/406192/get-current-url-with-jquery
Expand Down
141 changes: 25 additions & 116 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ $(document).on(
function () {
var words = getExcludedWords();

console.log(words);
browser.storage.local.set({
wordsToExclude: words,
});
Expand All @@ -78,134 +77,44 @@ $(document).on(
}
);

// setting the value of checkbox
browser.storage.local.get(shouldCapitaliseI).then((items) => {
const shouldCapitaliseI = items.shouldCapitaliseI;

if (shouldCapitaliseI === true || shouldCapitaliseI === undefined) {
//value not set yet/ext just installed
$('#shouldCapitaliseI').prop('checked', true);
setShouldCapitaliseIVariable(true);
} else {
$('#shouldCapitaliseI').prop('checked', false);
setShouldCapitaliseIVariable(false);
}
});

browser.storage.local.get(shouldCapitaliseNames).then((items) => {
const shouldCapitaliseNames = items.shouldCapitaliseNames;

if (shouldCapitaliseNames === true || shouldCapitaliseNames === undefined) {
//value not set yet/ext just installed
$('#shouldCapitaliseNames').prop('checked', true);
setShouldCapitaliseNamesVariable(true);
} else {
$('#shouldCapitaliseNames').prop('checked', false);
setShouldCapitaliseNamesVariable(false);
}
});

browser.storage.local.get(shouldCapitaliseAbbreviations).then((items) => {
const shouldCapitaliseAbbreviations = items.shouldCapitaliseAbbreviations;

if (
shouldCapitaliseAbbreviations === true ||
shouldCapitaliseAbbreviations === undefined
) {
//value not set yet/ext just installed
$('#shouldCapitaliseAbbreviations').prop('checked', true);
setShouldCapitaliseAbbreviationsVariable(true);
} else {
$('#shouldCapitaliseAbbreviations').prop('checked', false);
setShouldCapitaliseAbbreviationsVariable(false);
}
});

browser.storage.local.get(shouldCapitaliseLocations).then((items) => {
const shouldCapitaliseLocations = items.shouldCapitaliseLocations;

if (
shouldCapitaliseLocations === true ||
shouldCapitaliseLocations === undefined
) {
//value not set yet/ext just installed
$('#shouldCapitaliseLocations').prop('checked', true);
setShouldCapitaliseLocationsVariable(true);
} else {
$('#shouldCapitaliseLocations').prop('checked', false);
setShouldCapitaliseLocationsVariable(false);
}
});
loadFlagValuesFromBrowserStorage(shouldCapitaliseI);
loadFlagValuesFromBrowserStorage(shouldCapitaliseNames);
loadFlagValuesFromBrowserStorage(shouldCapitaliseAbbreviations);
loadFlagValuesFromBrowserStorage(shouldCapitaliseLocations);

$(document).on(
`change.${pluginNamespace}`,
'#shouldCapitaliseI',
function (event) {
if ($(event.target).prop('checked')) {
setShouldCapitaliseIVariable(true);
} else {
setShouldCapitaliseIVariable(false);
}
}
);
function loadFlagValuesFromBrowserStorage(flagName) {
browser.storage.local.get(flagName).then((items) => {
const flagValue = items[flagName];

$(document).on(
`change.${pluginNamespace}`,
'#shouldCapitaliseAbbreviations',
function (event) {
if ($(event.target).prop('checked')) {
setShouldCapitaliseAbbreviationsVariable(true);
if (flagValue === true || flagValue === undefined) {
//value not set yet/ext just installed
$(`#${flagName}`).prop('checked', true);
setShouldCapitaliseVariable(flagName, true);
} else {
setShouldCapitaliseAbbreviationsVariable(false);
$(`#${flagName}`).prop('checked', false);
setShouldCapitaliseVariable(flagName, false);
}
}
);
});
}

$(document).on(
`change.${pluginNamespace}`,
'#shouldCapitaliseLocations',
function (event) {
if ($(event.target).prop('checked')) {
setShouldCapitaliseLocationsVariable(true);
} else {
setShouldCapitaliseLocationsVariable(false);
}
}
);
setupCheckboxChangeEventHandlers(shouldCapitaliseI);
setupCheckboxChangeEventHandlers(shouldCapitaliseNames);
setupCheckboxChangeEventHandlers(shouldCapitaliseAbbreviations);
setupCheckboxChangeEventHandlers(shouldCapitaliseLocations);

$(document).on(
`change.${pluginNamespace}`,
'#shouldCapitaliseNames',
function (event) {
function setupCheckboxChangeEventHandlers(flagName) {
$(document).on('change', `#${flagName}`, function (event) {
if ($(event.target).prop('checked')) {
setShouldCapitaliseNamesVariable(true);
setShouldCapitaliseVariable(flagName, true);
} else {
setShouldCapitaliseNamesVariable(false);
setShouldCapitaliseVariable(flagName, false);
}
}
);

function setShouldCapitaliseIVariable(value) {
browser.storage.local.set({
shouldCapitaliseI: value,
});
}

function setShouldCapitaliseNamesVariable(value) {
browser.storage.local.set({
shouldCapitaliseNames: value,
});
}

function setShouldCapitaliseAbbreviationsVariable(value) {
browser.storage.local.set({
shouldCapitaliseAbbreviations: value,
});
}

function setShouldCapitaliseLocationsVariable(value) {
function setShouldCapitaliseVariable(variableName, value) {
browser.storage.local.set({
shouldCapitaliseLocations: value,
[variableName]: value,
});
}

Expand Down
Loading

0 comments on commit 387ee1f

Please sign in to comment.