Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fixes #5089 - Screenshots keyboard shortcut
Browse files Browse the repository at this point in the history
TIL Can't specify 'option' as a webextension hot key.
Just go with ctrl + shift + s, which will be command + shift + s on mac.
  • Loading branch information
jaredhirsch authored and punamdahiya committed Jan 18, 2019
1 parent cb3ba84 commit dc35fa0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
66 changes: 33 additions & 33 deletions webextension/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,65 +94,65 @@ this.main = (function() {
// This is called by startBackground.js, where is registered as a click
// handler for the webextension page action.
exports.onClicked = catcher.watchFunction((tab) => {
catcher.watchPromise(hasSeenOnboarding.then(onboarded => {
if (shouldOpenMyShots(tab.url)) {
if (!onboarded) {
catcher.watchPromise(analytics.refreshTelemetryPref().then(() => {
sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito});
return forceOnboarding();
}));
return;
}
catcher.watchPromise(analytics.refreshTelemetryPref().then(() => {
sendEvent("goto-myshots", "about-newtab", {incognito: tab.incognito});
}));
catcher.watchPromise(
auth.maybeLogin()
.then(() => browser.tabs.update({url: backend + "/shots"})));
} else {
catcher.watchPromise(
toggleSelector(tab)
.then(active => {
const event = active ? "start-shot" : "cancel-shot";
sendEvent(event, "toolbar-button", {incognito: tab.incognito});
}, (error) => {
if ((!onboarded) && error.popupMessage === "UNSHOOTABLE_PAGE") {
sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito});
return forceOnboarding();
}
throw error;
}));
}
}));
_startShotFlow(tab, "toolbar-button");
});

function forceOnboarding() {
return browser.tabs.create({url: getOnboardingUrl()});
}

exports.onClickedContextMenu = catcher.watchFunction((info, tab) => {
_startShotFlow(tab, "context-menu");
});

exports.onCommand = catcher.watchFunction((tab) => {
_startShotFlow(tab, "keyboard-shortcut");
});

const _openMyShots = (tab, inputType) => {
catcher.watchPromise(analytics.refreshTelemetryPref().then(() => {
sendEvent("goto-myshots", inputType, {incognito: tab.incognito});
}));
catcher.watchPromise(
auth.maybeLogin()
.then(() => browser.tabs.update({url: backend + "/shots"})));
};

const _startShotFlow = (tab, inputType) => {
catcher.watchPromise(hasSeenOnboarding.then(onboarded => {
if (!tab) {
// Not in a page/tab context, ignore
return;
}
if (!urlEnabled(tab.url)) {
if (!onboarded) {
sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito});
// Updated generic "selection-button" event data to inputType
sendEvent("goto-onboarding", inputType, {incognito: tab.incognito});
forceOnboarding();
return;
}
senderror.showError({
popupMessage: "UNSHOOTABLE_PAGE",
});
return;
} else if (shouldOpenMyShots(tab.url)) {
_openMyShots(tab, inputType);
return;
}
// No need to catch() here because of watchPromise().
// eslint-disable-next-line promise/catch-or-return
toggleSelector(tab)
.then(() => sendEvent("start-shot", "context-menu", {incognito: tab.incognito}));
.then(active => {
let event = "start-shot";
if (inputType !== "context-menu") {
event = active ? "start-shot" : "cancel-shot";
}
sendEvent(event, inputType, {incognito: tab.incognito});
}).catch((error) => {
throw error;
});
}));
});
};

function urlEnabled(url) {
if (shouldOpenMyShots(url)) {
Expand Down
16 changes: 16 additions & 0 deletions webextension/background/startBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ this.startBackground = (function() {
});
});

browser.commands.onCommand.addListener((cmd) => {
if (cmd !== "take-screenshot") {
return;
}
loadIfNecessary().then(() => {
browser.tabs.query({currentWindow: true, active: true}).then((tabs) => {
const activeTab = tabs[0];
main.onCommand(activeTab);
}).catch((error) => {
throw error;
});
}).catch((error) => {
console.error("Error toggling Screenshots via keyboard shortcut: ", error);
});
});

browser.runtime.onMessage.addListener((req, sender, sendResponse) => {
loadIfNecessary().then(() => {
return communication.onMessage(req, sender, sendResponse);
Expand Down
8 changes: 8 additions & 0 deletions webextension/manifest.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
"background/startBackground.js"
]
},
"commands": {
"take-screenshot": {
"suggested_key": {
"default": "Ctrl+Shift+S"
},
"description": "Open the Firefox Screenshots UI"
}
},
"content_scripts": [
{
"matches": ["http://localhost/*"],
Expand Down
5 changes: 4 additions & 1 deletion webextension/onboarding/slides.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ this.slides = (function() {
doc.documentElement.lang = browser.i18n.getMessage("@@ui_locale");
localizeText(doc);
activateSlide(doc);
// Give the DOM a moment to settle before applying focus
setTimeout(() => {
iframe.contentWindow.focus();
});
resolve();
}), {once: true});
document.body.appendChild(iframe);
iframe.focus();
window.addEventListener("resize", onResize);
});
};
Expand Down

0 comments on commit dc35fa0

Please sign in to comment.