From 2eb47c6d973c9ef1cb277fc22f4d15208e94f3c0 Mon Sep 17 00:00:00 2001 From: René Bertin Date: Wed, 11 Oct 2023 18:40:49 +0200 Subject: Ensure that links are opened according to the user's preference (#417) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Ensure that links are opened according to the user's preference This fixes the problem that certain links on instagram.com have started to open in the external browser. Also fixes an occasional nullptr dereference in getMessages(). Fixes: https://github.com/ferdium/ferdium-app/issues/1304 * Version bump for PR#417 * attempt to fix build by bumping recipe version again * Fix lint and CI * Fix versioning --------- Co-authored-by: MCMXC <16797721+mcmxcdev@users.noreply.github.com> Co-authored-by: André Oliveira --- recipes/instagram/package.json | 2 +- recipes/instagram/webview.js | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) (limited to 'recipes') diff --git a/recipes/instagram/package.json b/recipes/instagram/package.json index 5c798f3..9bdc70b 100644 --- a/recipes/instagram/package.json +++ b/recipes/instagram/package.json @@ -1,7 +1,7 @@ { "id": "instagram", "name": "Instagram", - "version": "2.5.0", + "version": "2.5.1", "license": "MIT", "config": { "serviceURL": "https://instagram.com/direct/inbox", diff --git a/recipes/instagram/webview.js b/recipes/instagram/webview.js index a4a5a8a..1b35c37 100644 --- a/recipes/instagram/webview.js +++ b/recipes/instagram/webview.js @@ -4,12 +4,43 @@ function _interopRequireDefault(obj) { const _path = _interopRequireDefault(require('path')); -module.exports = Ferdium => { +module.exports = (Ferdium, settings) => { + // adapted from the franz-custom-website recipe, for opening + // links according to the user's preference (Ferdium/ext.browser) + document.addEventListener( + 'click', + event => { + const link = event.target.closest('a'); + const button = event.target.closest('button'); + + if (link || button) { + const url = link + ? link.getAttribute('href') + : button.getAttribute('title'); + + // check if we have a valid URL that is not a script nor an image: + if (url && url !== '#' && !Ferdium.isImage(link)) { + event.preventDefault(); + event.stopPropagation(); + + if (settings.trapLinkClicks === true) { + window.location.href = url; + } else { + Ferdium.openNewWindow(url); + } + } + } + }, + true, + ); + const getMessages = () => { const element = document.querySelector('a[href^="/direct/inbox"]'); - Ferdium.setBadge( - element.textContent ? Ferdium.safeParseInt(element.textContent) : 0, - ); + if (element) { + Ferdium.setBadge( + element.textContent ? Ferdium.safeParseInt(element.textContent) : 0, + ); + } }; Ferdium.loop(getMessages); -- cgit v1.2.3-54-g00ecf