Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bolded tags lazily #82

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/Background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ const listener = (request, sender, sendResponse) => {
switch (request.message) {
case 'getToggleOnDefault': {
const toggleOnDefault = localStorage.getItem('toggleOnDefault');
console.log('getToggleOnDefault in BG returning=>', toggleOnDefault);
sendResponse({ data: toggleOnDefault });
break;
}
case 'setToggleOnDefault': {
console.log('setToggleOnDefault in BG data=>', request.data);
localStorage.setItem('toggleOnDefault', request.data);
sendResponse({ success: true });
break;
}
case 'getSaccadesInterval': {
const saccadesInterval = localStorage.getItem('saccadesInterval');
console.log('getSaccadesInterval in BG returning=>', saccadesInterval);
sendResponse({ data: saccadesInterval });
break;
}
case 'setSaccadesInterval': {
console.log('setToggleOnDefault in BG data=>', request.data);
localStorage.setItem('saccadesInterval', request.data);
sendResponse({ success: true });
break;
Expand All @@ -36,7 +32,6 @@ const listener = (request, sender, sendResponse) => {
break;
}
default:
console.log('Error: not found', request);
break;
}
};
Expand Down
26 changes: 8 additions & 18 deletions src/ContentScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ function makeFixations(/** @type string */ textContent) {
const mildFixation = ((textContent.length - (fixationWidth * 2)) > 0)
? `<br-fixation fixation-strength="2">${textContent.substring(fixationWidth, (textContent.length) - fixationWidth)}</br-fixation>` : '';

// console.table({
// textContent, weakFixation, mildFixation, strongFixation, fixationWidth,
// });
return weakFixation + mildFixation + strongFixation;
}

Expand All @@ -56,14 +53,12 @@ function parseDocument() {

const ToggleReading = (enableReading) => {
console.time('ToggleReading-Time');
console.log('enableReading =>', enableReading);
const boldedElements = document.getElementsByTagName('br-bold');

// if (enableReading === true) {
// document.body.classList.add('br-bold');
// } else {
// document.body.classList.toggle('br-bold');
// }
if (boldedElements.length < 1) {
addStyles();
parseDocument();
}

if (document.body.classList.contains('br-bold') || enableReading === false) {
document.body.classList.remove('br-bold');
Expand All @@ -85,7 +80,7 @@ const ToggleReading = (enableReading) => {
};

const onChromeRuntimeMessage = (message, sender, sendResponse) => {
console.log('Got msge in content script as =>', message, sender);
console.log('Got message in content script as =>', message, sender);
switch (message.type) {
case 'getBrMode':
sendResponse({ data: document.body.classList.contains('br-bold') });
Expand Down Expand Up @@ -132,10 +127,8 @@ const onChromeRuntimeMessage = (message, sender, sendResponse) => {
break;

default:
console.log('match not found');
break;
}
console.log('Setting currentHeight : ', currentHeight);
if (/\d+/.test(currentHeight)) {
document.body.style.setProperty(LINE_HEIGHT_KEY, currentHeight);
} else {
Expand All @@ -162,7 +155,7 @@ function docReady(fn) {
}
}

docReady(async () => {
function addStyles() {
const style = document.createElement('style');
style.textContent = `
.br-bold[fixation-strength="1"] :is(
Expand Down Expand Up @@ -215,23 +208,21 @@ docReady(async () => {
}
`;
document.head.appendChild(style);
}

parseDocument();

docReady(async () => {
runTimeHandler.runtime.onMessage.addListener(onChromeRuntimeMessage);

chrome.runtime.sendMessage(
{ message: 'getToggleOnDefault' },
(response) => {
console.log('getToggleOnDefault response=> ', response);
if (!['true', true].includes(response.data)) return;
ToggleReading(response.data === 'true');
},
);
chrome.runtime.sendMessage(
{ message: 'getSaccadesInterval' },
(response) => {
console.log('getSaccadesInterval response=> ', response);
const saccadesInterval = response === undefined || response.data == null
? DEFAULT_SACCADES_INTERVAL : response.data;
document.body.setAttribute('saccades-interval', saccadesInterval);
Expand All @@ -241,7 +232,6 @@ docReady(async () => {
chrome.runtime.sendMessage(
{ message: 'getFixationStrength' },
(response) => {
console.log('getFixationStrength response=> ', response);
const fixationStrength = response === undefined || response.data == null
? DEFAULT_FIXATION_STRENGTH : response.data;
document.body.setAttribute('fixation-strength', fixationStrength);
Expand Down
22 changes: 1 addition & 21 deletions src/Popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const fixationStrengthLabelValue = document.getElementById('fixationStrengthLabe
chrome.runtime.sendMessage(
{ message: 'getSaccadesInterval' },
(response) => {
console.log('getSaccadesInterval response in POP up=> ', response);

const saccadesInterval = response === undefined || response.data == null ? 0 : response.data;
const documentButtons = document.getElementsByTagName('button');
for (let index = 0; index < documentButtons.length; index++) {
Expand All @@ -31,13 +29,11 @@ chrome.runtime.sendMessage(
chrome.runtime.sendMessage(
{ message: 'getToggleOnDefault' },
(response) => {
console.log('getToggleOnDefault response in POP up => ', response);
toggleOnDefaultCheckbox.checked = response.data === 'true';
},
);

chrome.tabs.query({ active: true }, ([tab]) => {
console.log(tab);
chrome.tabs.sendMessage(tab.id, {
message: 'getBrMode', type: 'getBrMode',
}, (request) => {
Expand Down Expand Up @@ -71,23 +67,6 @@ toggleOnDefaultCheckbox.addEventListener('change', async (event) => {
(response) => {
},
);
// chrome.tabs.query({}, (tabs) => {
// tabs.forEach((tab) => new Promise(() => {
// try {
// chrome.tabs.sendMessage(
// tab.id,
// { type: 'setReadingMode', data: event.target.checked },
// () => {
// if (chrome.runtime.lastError) {
// // no-op
// }
// },
// );
// } catch (e) {
// // no-op
// }
// }));
// });
});

async function updateLineHeightClickHandler(event) {
Expand Down Expand Up @@ -148,6 +127,7 @@ fixationStrengthSlider.addEventListener('change', (event) => {
console.log(response);
});
});

/**
* @description Show the word interval between saccades
*/
Expand Down