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

Add notification and focus options #245

Merged
merged 12 commits into from
Mar 2, 2023
Prev Previous commit
Next Next commit
hide notification messages on (dis)connect
eugenesvk committed Mar 2, 2023
commit 6868eb141412221611644d6ab99f0143cca16ab1
2 changes: 1 addition & 1 deletion source/background.js
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ function handlePortListenerErrors(listener) {

chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
console.assert(port.name === 'new-field');
const {serverPort, focusOnDisconnect} = await optionsStorage.getAll();
const {serverPort, notifyOnConnect, focusOnDisconnect} = await optionsStorage.getAll();
const response = await fetch(`http://localhost:${serverPort}`);
const {ProtocolVersion, WebSocketPort} = await response.json();
if (ProtocolVersion !== 1) {
14 changes: 10 additions & 4 deletions source/ghost-text.js
Original file line number Diff line number Diff line change
@@ -113,14 +113,17 @@ class GhostTextField {
this.field.dataset.gtField = 'loading';

this.port = chrome.runtime.connect({name: 'new-field'});
this.port.onMessage.addListener(message => {
this.port.onMessage.addListener(async message => {
if (message.message) {
this.receive({data: message.message});
} else if (message.close) {
this.deactivate(false);
updateCount();
} else if (message.ready) {
notify('log', 'Connected! You can switch to your editor');
const options = await extOptions;
if (options.notifyOnConnect) {
notify('log', 'Connected! You can switch to your editor');
}

this.field.addEventListener('input', this.send);
this.field.dataset.gtField = 'enabled';
@@ -226,14 +229,17 @@ class GhostTextField {
}
}

function updateCount() {
async function updateCount() {
chrome.runtime.sendMessage({
code: 'connection-count',
count: activeFields.size,
});

if (activeFields.size === 0) {
notify('log', 'Disconnected! \n <a href="https://github.com/fregante/GhostText/issues" target="_blank">Report issues</a>');
const options = await extOptions;
if (options.notifyOnConnect) {
notify('log', 'Disconnected! \n <a href="https://github.com/fregante/GhostText/issues" target="_blank">Report issues</a>');
}
}
}