Skip to content

Commit

Permalink
Use the built-in keyboard commands system
Browse files Browse the repository at this point in the history
Registration and conflict resolution is simplified. Change default to
one that's unused by Firefox itself. Simplifies code but requires an
extra permission to find current tab.
  • Loading branch information
Roy-Orbison committed Jun 13, 2023
1 parent 41fe3de commit c047e59
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 286 deletions.
15 changes: 14 additions & 1 deletion webex/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function assertNoResponse(response) {
}

function notifyError(error) {
browser.notifications.create({
browser.notifications?.create({
type: "basic",
title: "Textern",
message: "Error: " + error + "."
Expand Down Expand Up @@ -126,3 +126,16 @@ function onMessage(message, sender, respond) {
}

browser.runtime.onMessage.addListener(onMessage);

browser.commands.onCommand.addListener(function(command) {
if (command === 'textern') {
browser.tabs.query({
currentWindow: true,
active: true
}).then(function(tabs) {
if (tabs?.length && tabs[0].id != browser.tabs.TAB_ID_NONE) {
browser.tabs.sendMessage(tabs[0].id, {type: 'shortcut'}).then(assertNoResponse, logError);
}
});
}
});
59 changes: 26 additions & 33 deletions webex/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function assertNoResponse(response) {
}

function notifyError(error) {
browser.notifications.create({
browser.notifications?.create({
type: "basic",
title: "Textern",
message: "Error: " + error + "."
Expand Down Expand Up @@ -91,8 +91,7 @@ function slackSetText(e, text) {
}
}

function registerText(event) {
var e = event.target;
function registerText(e) {
if (e.nodeName == "TEXTAREA") {
var id = watchElement(e);
/* don't use href directly to not bring in e.g. url params */
Expand Down Expand Up @@ -171,43 +170,37 @@ function setText(id, text) {
fadeBackground(e);
}

function getActiveElement(element = document.activeElement) {
if (element.nodeName === 'IFRAME' || element.nodeName === 'FRAME')
return null; // skip to allow child documents to handle shortcut

const shadowRoot = element.shadowRoot
const contentDocument = element.contentDocument

if (shadowRoot && shadowRoot.activeElement) {
return getActiveElement(shadowRoot.activeElement)
}

if (contentDocument && contentDocument.activeElement) {
return getActiveElement(contentDocument.activeElement)
}

return element
}

function onMessage(message, sender, respond) {
if (sender.id != "textern@jlebon.com")
return;
if (message.type == "set_text")
if (message.type == 'shortcut') {
const activeElement = getActiveElement();
if (activeElement)
registerText(activeElement);
}
else if (message.type == "set_text")
setText(message.id, message.text);
else {
console.log(`Unknown message type: ${message.type}`);
}
}

browser.runtime.onMessage.addListener(onMessage);

var currentShortcut = undefined;
function registerShortcut(force) {
browser.storage.local.get({shortcut: "Ctrl+Shift+D"}).then(val => {
if ((val.shortcut == currentShortcut) && !force)
return; /* no change */
if (currentShortcut != undefined)
shortcut.remove(currentShortcut);
currentShortcut = val.shortcut;
shortcut.add(currentShortcut, registerText);
});
}

registerShortcut(true);

/* meh, we just re-apply the shortcut -- XXX: should check what actually changed */
browser.storage.onChanged.addListener(function(changes, areaName) {
registerShortcut(false);
});

/* we also want to make sure we re-register whenever the number of iframes changes */
var lastNumFrames = window.frames.length;
const observer = new MutationObserver(function() {
if (window.frames.length != lastNumFrames) {
registerShortcut(true);
lastNumFrames = window.frames.length;
}
});
observer.observe(document, {childList: true, subtree: true});
29 changes: 23 additions & 6 deletions webex/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Edit text in your favourite external editor!",
"homepage_url": "https://github.com/jlebon/textern",
"manifest_version": 2,
"version": "0.8",
"version": "0.9b1",

"browser_specific_settings": {
"gecko": {
Expand All @@ -17,15 +17,32 @@
},

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["shortcut.js", "content.js"]
}
{
"matches": ["<all_urls>"],
"js": [
"content.js"
],
"all_frames": true
}
],

"commands": {
"textern": {
"suggested_key": {
"default": "Shift+Alt+D"
},
"description": "Edit externally"
}
},

"options_ui": {
"page": "options.html"
},

"permissions": ["notifications", "nativeMessaging", "storage"]
"permissions": [
"tabs",
"notifications",
"nativeMessaging",
"storage"
]
}
3 changes: 1 addition & 2 deletions webex/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
<input type="text" id="editor" required=true
pattern='\[ *"[^"]*"(, *"[^"]*")* *\]'
title='Must be in the form: ["executable", "arg", "arg", ...]'>
<label for="shortcut">Shortcut</label>
<input type="text" id="shortcut" required=true>
<label for="extension">Document extension</label>
<input type="text" id="extension" required=true pattern="[.a-zA-Z0-9_-]+"
title='Must be an alphanumerical string (excluding leading dot)'>
<label for="backupdir">Backup files to directory (experimental)</label>
<input type="text" id="backupdir">
<button type="submit">Save</button><span id="saved"></span>
</form>
<p>Shortcut can be configured on the Manage Extension Shortcuts settings page.</p>
<script src="options.js" charset="utf-8"></script>
</body>
</html>
5 changes: 0 additions & 5 deletions webex/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function saveOptions(e) {
e.preventDefault();
browser.storage.local.set({
editor: document.querySelector("#editor").value,
shortcut: document.querySelector("#shortcut").value,
extension: document.querySelector("#extension").value,
backupdir: document.querySelector("#backupdir").value
});
Expand All @@ -32,10 +31,6 @@ function restoreOptions() {
result.editor || "[\"gedit\", \"+%l:%c\"]";
}, onError);

browser.storage.local.get("shortcut").then(result => {
document.querySelector("#shortcut").value = result.shortcut || "Ctrl+Shift+D";
}, onError);

browser.storage.local.get("extension").then(result => {
document.querySelector("#extension").value = result.extension || "txt";
}, onError);
Expand Down
Loading

0 comments on commit c047e59

Please sign in to comment.