Skip to content

Commit

Permalink
Rewrite upstream chrome addon to use autohotkey to provide firefox an…
Browse files Browse the repository at this point in the history
…d mouse support
  • Loading branch information
lukefor committed Jul 31, 2022
1 parent b2a6ea9 commit 5bcef0b
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 318 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/native.exe
23 changes: 0 additions & 23 deletions README

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This Firefox extension allows the Discord web client push-to-talk (PTT) mode to operate system-wide.
An Autohotkey script listens for configurable shortcut, then passes this to the extension, which emulates PTT key events in Discord.

### Installation:

1. Update location in `native.reg` to match where you have cloned to
1. Apply the `native.reg` file
1. Edit `native.ahk` if you wish to update the shortcut key (default is `Mouse 5`)
1. Compile `native.ahk` to exe with AutoHotkey
1. Restart Firefox
1. Install extension in Firefox
1. Enable PTT in Discord and set the PTT key in Discord web settings to `Ctrl+Alt+Shift+P`.
84 changes: 25 additions & 59 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* The default minimum number of ms that any 'PTT active' window can last.
* @const {number}
*/
const MIN_PTT_LENGTH_DEFAULT = 800;
const MIN_PTT_LENGTH_DEFAULT = 100;


console.log("hello background");


/**
* Runs the given callback with the tab ID for the broadcasting Discord tab, if
Expand All @@ -12,52 +16,14 @@ const MIN_PTT_LENGTH_DEFAULT = 800;
* broadcasting Discord tab ID.
*/
function withBroadcastingTab(cb) {
chrome.storage.local.get('broadcastingTab', function(result) {
browser.storage.local.get('broadcastingTab', function(result) {
if (result.broadcastingTab != null) {
cb(result.broadcastingTab);
}
});
}

/**
* Propogates the stored min PTT length, or the default (if no value has been stored).
*
* @param {function(number)} cb - A callback that will be run with the stored
* min PTT length as its argument.
* @return {boolean} true if cb will be called asynchronously.
*/
function sendMinPttLength(cb) {
chrome.storage.local.get('minPttLength', function(result) {
cb(result.minPttLength != null ? result.minPttLength : MIN_PTT_LENGTH_DEFAULT);
});
return true;
}

/**
* Stores the new min PTT length and updates all popups and Discord tabs about
* the change.
*
* @param {number} minPttLength - The new minimum PTT length.
*/
function onMinPttLengthChanged(minPttLength) {
chrome.storage.local.set({
minPttLength: minPttLength,
});

// Send message to all popups.
chrome.runtime.sendMessage({
id: 'min_ptt_length_changed',
value: minPttLength,
});

// Send message to Discord tab.
withBroadcastingTab(function(id) {
chrome.tabs.sendMessage(id, {
id: 'min_ptt_length_changed',
value: minPttLength,
});
});
}
var port;

/**
* Updates extension badge when a Discord tab starts / stops broadcasting.
Expand All @@ -67,33 +33,42 @@ function onMinPttLengthChanged(minPttLength) {
* has started.
*/
function onBroadcastingNotice(id, broadcasting) {
chrome.storage.local.get('broadcastingTab', function(result) {
browser.storage.local.get('broadcastingTab', function(result) {
if (broadcasting) {
chrome.browserAction.setBadgeText({
browser.browserAction.setBadgeText({
text: 'ON',
});

chrome.storage.local.set({
browser.storage.local.set({
broadcastingTab: id,
});

port = browser.runtime.connectNative("discord_web_sys_ptt_native");
port.onMessage.addListener((response) => {
console.log(response);
withBroadcastingTab(function(id) {
browser.tabs.sendMessage(id, {
id: 'ext_shortcut_pushed',
});
});
});
} else if (result.broadcastingTab === id) {
chrome.browserAction.setBadgeText({
browser.browserAction.setBadgeText({
text: '',
});

chrome.storage.local.set({
browser.storage.local.set({
broadcastingTab: null,
});

port.disconnect();
}
});
}

// Handle messages from Discord tabs and popups.
chrome.runtime.onMessage.addListener(function(msg, sender, cb) {
browser.runtime.onMessage.addListener(function(msg, sender, cb) {
if (msg.id === 'discord_loaded' || msg.id === 'popup_loaded') {
return sendMinPttLength(cb);
} else if (msg.id === 'min_ptt_length_changed') {
onMinPttLengthChanged(msg.value);
return false;
} else if (msg.id === 'broadcasting' && sender != null &&
sender.tab != null && sender.tab.id != null) {
Expand All @@ -103,12 +78,3 @@ chrome.runtime.onMessage.addListener(function(msg, sender, cb) {

return false;
});

// When extension shortcut is pressed, notify Discord tab.
chrome.commands.onCommand.addListener(function() {
withBroadcastingTab(function(id) {
chrome.tabs.sendMessage(id, {
id: 'ext_shortcut_pushed',
});
});
});
Loading

0 comments on commit 5bcef0b

Please sign in to comment.