Skip to content

Commit

Permalink
ABBREVIATIONS-FLAG Added Settings Flag For Abbreviations (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrai committed Jan 18, 2021
1 parent 8be593a commit 302bd12
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 121 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ Firefox/Microsoft Edge Chromium add-on to automatically capitalise words while t

- Capitalise the first letter of a sentence.
- Capitalise `I`.
- Capitalise constants such as [days](src/constants.js#L3), [months](src/constants.js#L13)
- Capitalise common [abbreviations](src/constants.js#L78)
- Capitalise constants such as [days](src/constants.js#L4), [months](src/constants.js#L14)
- Capitalise common [abbreviations](src/abbreviation-constants.js#L1)
- Capitalise common [names](src/name-constants.js#L1)
- Add apostrophe to common [English words](src/constants.js#L81)

## Configuration

Expand Down
3 changes: 2 additions & 1 deletion distribution/dependencies/jquery.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distribution/lib/background.bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distribution/lib/main.bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distribution/lib/settings.bundle.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions distribution/popup/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
<input type="checkbox" id="shouldCapitaliseI" name="shouldCapitaliseI" checked>
<label for="shouldCapitaliseI">Should capitalise 'I'?</label><br>

<input type="checkbox" id="shouldCapitaliseAbbreviations" name="shouldCapitaliseAbbreviations" checked>
<label for="shouldCapitaliseAbbreviations">Should capitalise abbreviations?</label><br>

<input type="checkbox" id="shouldCapitaliseNames" name="shouldCapitaliseNames" checked>
<label for="shouldCapitaliseNames">Should capitalise names?</label><br>

</div>
</div>
</body>
Expand Down
Binary file modified imgs/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/settings.xcf
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint": "^7.11.0",
"eslint-webpack-plugin": "^2.1.0",
"express": "^4.17.1",
"express-rate-limit": "^5.2.3",
"husky": "^4.3.0",
"jest": "^26.5.3",
"jquery": "^3.5.1",
Expand Down Expand Up @@ -75,7 +76,7 @@
"run": {
"keepProfileChanges": true,
"startUrl": [
"github.com"
"http://localhost:3000"
]
}
}
Expand Down
81 changes: 81 additions & 0 deletions src/abbreviation-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const common_abbreviations = [
'AFR',
'AMD',
'AOL',
'APM',
'ATM',
'AWS',
'BBB',
'BMW',
'BP',
'BRB',
'BSD',
'BTW',
'CRE',
'CVS',
'DIY',
'FAQ',
'FDR',
'FNMA',
'FSF',
'FTW',
'FYI',
'GE',
'GNU',
'GTE',
'GTG',
'HBO',
'HSBC',
'HTML',
'IBM',
'ICYMI',
'IDK',
'IKEA',
'IMO',
'IOW',
'ISO',
'ITT',
'JFK',
'KFC',
'LGTM',
'LOL',
'MCI',
'MGM',
'MIT',
'MMW',
'MSDN',
'NASCAR',
'NORAD',
'NP',
'NSA',
'NVIDIA',
'NW',
'OMG',
'OTOH',
'POV',
'RCA',
'RDS',
'ROTFL',
'RSVP',
'SARS',
'SMH',
'TBA',
'TBC',
'TBH',
'TC',
'TGIF',
'THX',
'TIA',
'TTYL',
'TWA',
'UBS',
'UCLA',
'UPS',
'USB',
'WFH',
'WSL',
'WTF',
'WTH',
];

export const abbreviations = common_abbreviations;
3 changes: 2 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { constants_key_val, names_key_val } from './constants';
import { constants_key_val, names_key_val, abbreviations_key_val } from './constants';
import browser from 'webextension-polyfill';

browser.storage.local.set({
constants_key_val: constants_key_val,
names_key_val: names_key_val,
abbreviations_key_val: abbreviations_key_val,
});
94 changes: 8 additions & 86 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { names } from './name-constants'
import { abbreviations } from './abbreviation-constants'

const days = [
'Monday',
Expand Down Expand Up @@ -77,85 +78,6 @@ const common_tech_words = [
'iTunes',
]

const abbreviations = [
'AFR',
'AMD',
'AOL',
'APM',
'ATM',
'AWS',
'BBB',
'BMW',
'BP',
'BRB',
'BSD',
'BTW',
'CRE',
'CVS',
'DIY',
'FAQ',
'FDR',
'FNMA',
'FSF',
'FTW',
'FYI',
'GE',
'GNU',
'GTE',
'GTG',
'HBO',
'HSBC',
'IBM',
'ICYMI',
'IDK',
'IKEA',
'IMO',
'IOW',
'ISO',
'ITT',
'JFK',
'KFC',
'LGTM',
'LOL',
'MCI',
'MGM',
'MIT',
'MMW',
'MSDN',
'NASCAR',
'NORAD',
'NP',
'NSA',
'NVIDIA',
'NW',
'OMG',
'OTOH',
'POV',
'RCA',
'RDS',
'ROTFL',
'RSVP',
'SARS',
'SMH',
'TBA',
'TBC',
'TBH',
'TC',
'TGIF',
'THX',
'TIA',
'TTYL',
'TWA',
'UBS',
'UCLA',
'UPS',
'USB',
'WFH',
'WSL',
'WTF',
'WTH',
]

let words_with_apostrophe = {
arent: "aren't",
doesnt: "doesn't",
Expand All @@ -173,17 +95,17 @@ let words_with_apostrophe = {
wouldnt: "wouldn't",
}

let constants = days.concat(months, abbreviations, common_tech_words)
let constants = days.concat(months, common_tech_words)

let constants_map = constants.reduce((obj, val) => {
let string_to_key_val = (obj, val) => {
obj[val.toLowerCase()] = val
return obj
}, {})
}

let constants_map = constants.reduce(string_to_key_val, {})

//convert array to key-value pairs
export let constants_key_val = { ...constants_map, ...words_with_apostrophe }

export let names_key_val = names.reduce((obj, val) => {
obj[val.toLowerCase()] = val
return obj
}, {})
export let names_key_val = names.reduce(string_to_key_val, {})
export let abbreviations_key_val = abbreviations.reduce(string_to_key_val, {})
42 changes: 28 additions & 14 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
sites_to_ignore,
should_capitalise_i,
should_capitalise_names,
should_capitalise_abbreviations,
constants_key_val,
names_key_val,
abbreviations_key_val,
} from './plugin-constants';

const errorMsg = 'breaking loop';
Expand All @@ -17,16 +19,18 @@ browser.storage.local
sites_to_ignore,
should_capitalise_i,
should_capitalise_names,
should_capitalise_abbreviations,
constants_key_val,
names_key_val,
abbreviations_key_val,
])
.then(processResponse, utils.onError);

/* 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 dfeault after installing the extension.
* This block will capture the event and update the value of 'should_capitalise_i'.
*/
browser.storage.onChanged.addListener(function(
browser.storage.onChanged.addListener(function (
changes, // object
areaName // string
) {
Expand All @@ -46,6 +50,14 @@ browser.storage.onChanged.addListener(function(
utils.setShouldCapitaliseNames(newValue);
}
}

if (changes.should_capitalise_abbreviations != null) {
const newValue = changes.should_capitalise_abbreviations.newValue;

if (newValue != null) {
utils.setShouldCapitaliseAbbreviations(newValue);
}
}
}
});

Expand All @@ -57,23 +69,23 @@ function hookupEventHandlers() {
}

function observeIframeInputTags() {
$('iframe').on('load', event => {
$('iframe').on('load', (event) => {
let iframe = event.target;
$(iframe)
.contents()
.find(':text,textarea')
.each((_, item) => {
//console.log(item);

$(item).on(`input.${pluginNamespace}`, function(event) {
$(item).on(`input.${pluginNamespace}`, function (event) {
capitaliseText(event.target);
});
});
});
}

function observeInputTags() {
$(':text,textarea').on(`input.${pluginNamespace}`, function(event) {
$(':text,textarea').on(`input.${pluginNamespace}`, function (event) {
capitaliseText(event.target);
});
}
Expand All @@ -82,8 +94,10 @@ function processResponse(item) {
sitesToExclude = item.sites_to_ignore;
utils.setShouldCapitaliseI(item.should_capitalise_i);
utils.setShouldCapitaliseNames(item.should_capitalise_names);
utils.setShouldCapitaliseAbbreviations(item.should_capitalise_abbreviations);
utils.setConstantsKeyVal(item.constants_key_val);
utils.setNamesKeyVal(item.names_key_val);
utils.setAbbreviationsKeyVal(item.abbreviations_key_val);

if (item && sitesToExclude) {
//https://stackoverflow.com/questions/406192/get-current-url-with-jquery
Expand All @@ -92,7 +106,7 @@ function processResponse(item) {
try {
var shouldEnableCapitalisingOnCurrentSite = true;

$.each(sitesToExclude, function(_i, siteToExclude) {
$.each(sitesToExclude, function (_i, siteToExclude) {
if (currentUrlDomain.includes(siteToExclude)) {
shouldEnableCapitalisingOnCurrentSite = false;
}
Expand Down Expand Up @@ -120,8 +134,8 @@ function observeHtmlBody() {
var tags = ['p', 'span'];
var inputTags = ['input[type=\'text\']', 'textarea'];

var observer = new MutationObserver(function(mutations) {
$.each(mutations, function(_i, mutation) {
var observer = new MutationObserver(function (mutations) {
$.each(mutations, function (_i, mutation) {
try {
if (mutation.type === 'childList') {
// add support for div block in gmail and outlook
Expand All @@ -132,30 +146,30 @@ function observeHtmlBody() {

var addedNodes = mutation.addedNodes;
if (addedNodes && addedNodes.length > 0) {
addedNodes.forEach(node => {
addedNodes.forEach((node) => {
if (utils.isFirstTextOfEditableTextNode(node)) {
capitaliseText(node.parentNode);
addedNodes = addedNodes.filter(addedNode => {
addedNodes = addedNodes.filter((addedNode) => {
addedNode != node;
});
}
});

$.each(tags, function(_i, tagName) {
$.each(tags, function (_i, tagName) {
var filteredEls = utils.getFilteredElements(addedNodes, tagName);

filteredEls.each(function(_index, element) {
filteredEls.each(function (_index, element) {
if (utils.shouldCapitaliseContent(element)) {
capitaliseText(element);
}
});
});

$.each(inputTags, function(_i, tagName) {
$.each(inputTags, function (_i, tagName) {
var filteredEls = utils.getFilteredElements(addedNodes, tagName);

filteredEls.each(function(_index, element) {
$(element).on(`input.${pluginNamespace}`, function(event) {
filteredEls.each(function (_index, element) {
$(element).on(`input.${pluginNamespace}`, function (event) {
capitaliseText(event.target);
});
});
Expand Down
Loading

0 comments on commit 302bd12

Please sign in to comment.