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 context-menu item for generating QR code for the current page (#321) #336

Merged
merged 10 commits into from
Oct 24, 2024
Merged
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Metta Ong ([@ongspxm](https://github.com/ongspxm))
- Michael Corwin ([@mcorwin22](https://github.com/mcorwin22))
- Pradyumna Mahajan ([@pradyumnamahajan](https://github.com/pradyumnamahajan))
- Mahmoud Ayesh ([@mahmoudhusam](https://github.com/mahmoudhusam))

## Translators

Expand Down
16 changes: 16 additions & 0 deletions src/_locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
"message": "&QR-Code aus Link",
"description": "The context menu entry shown for generating QR codes from a selected link with an access key."
},
"contextMenuItemConvertPageURL": {
"message": "Generiere QR-Code für diese Seite",
"description": "Context menu item title for generating a QR code for the current page URL."
},
"contextMenuItemConvertPageURLAccessKey": {
"message": "&Generiere QR-Code für diese Seite",
"description": "Context menu item title for generating a QR code for the current page URL with an access key."
},
"contextMenuSaveImageCanvas": {
"message": "QR-Code als Bild speichern…",
"description": "The context menu entry shown for saving SVG images in the popup."
Expand Down Expand Up @@ -241,6 +249,14 @@
"message": "Zeigt ein farbiges Icon anstatt eines schwarz/weißen Icons in der Toolbar.",
"description": "This is an option shown in the add-on settings. It describes the optionPopupIconColored setting in more details."
},
"optionContextMenuEnabled": {
"message": "Zeige allgemeinen QR-Code-Generierungs-Kontextmenü-Eintrag",
"description": "This label refers to an option in the add-on settings. It allows users to toggle the visibility of the context menu item for generating a QR code from the current page."
},
"optionContextMenuEnabledDescr": {
"message": "Zeigt einen Kontextmenü-Eintrag um QR-Codes für die aktuelle Seite zu erstellen, zusätzlich zu denen für Links und ausgewählten Text.",
"description": "This is the helper text for the context menu toggle option. It explains that enabling this option will show the QR code generation context menu for the current page."
},

"subtitleQrCodeSize": {
"message": "QR-Code-Größe",
Expand Down
16 changes: 16 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
"message": "&QR code from link",
"description": "The context menu entry shown for generating QR codes from a selected link with an access key."
},
"contextMenuItemConvertPageURL": {
"message": "Generate QR code for this page",
"description": "Context menu item title for generating a QR code for the current page URL."
},
"contextMenuItemConvertPageURLAccessKey": {
"message": "&Generate QR code for this page",
"description": "Context menu item title for generating a QR code for the current page URL with an access key."
},
"contextMenuSaveImageCanvas": {
"message": "Save QR code as an image…",
"description": "The context menu entry shown for saving PNG images (from a canvas) in the popup."
Expand Down Expand Up @@ -242,6 +250,14 @@
"message": "Shows a colored icon instead of the black/white icon in the toolbar.",
"description": "This is an option shown in the add-on settings. It describes the optionPopupIconColored setting in more details."
},
"optionContextMenuEnabled": {
"message": "Show general QR code generation context menu item",
"description": "This label refers to an option in the add-on settings. It allows users to toggle the visibility of the context menu item for generating a QR code from the current page."
},
"optionContextMenuEnabledDescr": {
"message": "Shows a context menu item to create QR code for the current page, in addition to links and text selections.",
"description": "This is the helper text for the context menu toggle option. It explains that enabling this option will show the QR code generation context menu for the current page."
},

"subtitleQrCodeSize": {
"message": "QR code size",
Expand Down
26 changes: 25 additions & 1 deletion src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createMenu } from "/common/modules/ContextMenu.js";
const CONVERT_TEXT_SELECTION = "qr-convert-text-selection";
const CONVERT_LINK_TEXT_SELECTION = "qr-convert-link-text-selection";
const OPEN_OPTIONS = "qr-open-options";
const CONVERT_PAGE_URL = "qr-convert-page-url";
Dismissed Show dismissed Hide dismissed

const MESSAGE_RESENT_TIMEOUT = 200; // ms
const MESSAGE_RESENT_MAX = 9;
Expand Down Expand Up @@ -55,12 +56,17 @@ function createItems() {
contexts: ["link"]
});

const pageMenu = createMenu("contextMenuItemConvertPageURL", {
Dismissed Show dismissed Hide dismissed
id: CONVERT_PAGE_URL,
contexts: ["page"]
});

browser.menus.refresh();

// if listener is set, because items were hidden -> remove it
browser.menus.onHidden.removeListener(createItems);

return Promise.all([selectionMenu, linkMenu]);
return Promise.all([selectionMenu, linkMenu, pageMenu]);
}

/**
Expand Down Expand Up @@ -88,6 +94,13 @@ function menuClicked(event) {
sendQrCodeText(event.linkUrl);
});
break;
case CONVERT_PAGE_URL:
browser.browserAction.openPopup().then(() => {
messageResentCount = 0;
// Send the current page URL to the popup explicitly to overwrite any setting that may use a different string
sendQrCodeText(event.pageUrl);
});
break;
case OPEN_OPTIONS:
browser.runtime.openOptionsPage();
break;
Expand Down Expand Up @@ -119,6 +132,7 @@ function menuShown(info) {

browser.menus.remove(CONVERT_TEXT_SELECTION);
browser.menus.remove(CONVERT_LINK_TEXT_SELECTION);
browser.menus.remove(CONVERT_PAGE_URL);

browser.menus.refresh();
}
Expand All @@ -135,5 +149,15 @@ export function init() {
return createItems().then(() => {
browser.menus.onClicked.addListener(menuClicked);
browser.menus.onShown.addListener(menuShown);
browser.runtime.onMessage.addListener((message) => {
if (message.action === "enableContextMenu") {
browser.menus.update(CONVERT_PAGE_URL, { visible: true });
console.log("Context menu enabled");
} else if (message.action === "disableContextMenu") {
browser.menus.update(CONVERT_PAGE_URL, { visible: false });
console.log("Context menu disabled");
}
browser.menus.refresh();
});
});
}
1 change: 1 addition & 0 deletions src/common/modules/data/DefaultSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
const defaultSettings = Object.freeze({
debugMode: false,
popupIconColored: false,
contextMenuEnabled: true,
qrColor: "#0c0c0d",
qrBackgroundColor: (isDarkTheme ? "#d7d7db" : "#ffffff"), // dark uses Firefox Photon's grey-30
qrErrorCorrection: "Q",
Expand Down
26 changes: 25 additions & 1 deletion src/options/modules/CustomOptionTriggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ function applyPopupIconColor(optionValue) {
IconHandler.changeIconIfColored(optionValue);
}

/**
* Adjusts UI based on the state of context menu enabled option.
*
* @function
* @private
* @param {boolean} optionValue
* @returns {void}
*/
function applyContextMenuEnabled(optionValue) {
if (optionValue) {
console.log("Context menu is enabled");
// Call the logic to enable the context menu
browser.runtime.sendMessage({ action: "enableContextMenu" });
} else {
console.log("Context menu is disabled");
// Call the logic to disable the context menu
browser.runtime.sendMessage({ action: "disableContextMenu" });
}
}

/**
* Adjust UI if QR code size option is changed.
*
Expand Down Expand Up @@ -254,10 +274,14 @@ export function registerTrigger() {
AutomaticSettings.Trigger.registerSave("qrBackgroundColor", applyQrCodeColors);
AutomaticSettings.Trigger.registerSave("qrQuietZone", updateQrQuietZoneStatus);

// Register trigger for contextMenuEnabled
AutomaticSettings.Trigger.registerSave("contextMenuEnabled", applyContextMenuEnabled);
AutomaticSettings.Trigger.registerUpdate("contextMenuEnabled", applyContextMenuEnabled);

AutomaticSettings.Trigger.registerUpdate("qrColor", applyQrCodeColors);
AutomaticSettings.Trigger.registerUpdate("qrBackgroundColor", applyQrCodeColors);

// handle loading of options correctly
AutomaticSettings.Trigger.registerBeforeLoad(resetOnBeforeLoad);
AutomaticSettings.Trigger.registerAfterLoad(AutomaticSettings.Trigger.RUN_ALL_SAVE_TRIGGER);
}
}
9 changes: 9 additions & 0 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ <h1 data-i18n="__MSG_titleGeneral__">Add-on</h1>
<span class="helper-text" data-i18n="__MSG_optionPopupIconColoredDescr__">Shows a colored icon instead of the black/white icon in the toolbar.</span>
</div>
</li>
<li>
<div class="line">
<input class="setting save-on-change" type="checkbox" id="contextMenuEnabled" name="contextMenuEnabled">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Could you add the required i18n attributes to enable localisation? Aka internationalize this part here?

Also provide the translation string in the message.json file, so people can translate it. It is enough to provide the English translation, of course, so you can just copy and paste the string from here.
This ensures, that text snippet is ready for translation and can be localized.

For more information, please see the MDN guide on how to localize WebExtensions and our internationalization (i18n) guide.

Also cf the rest of the options HTML of course.

<label data-i18n="__MSG_optionContextMenuEnabled__" for="contextMenuEnabled">Show general QR code generation context menu item</label>
</div>
<div class="line indent">
<span class="helper-text" data-i18n="__MSG_optionContextMenuEnabledDescr__">Shows a context menu item to create QR code for the current page, in addition to links and text selections.</span>
</div>
</li>
</ul>
</section>

Expand Down
Loading