From 4b4c9786447dd75852ee1ccc0c03eb0ef91e3d1d Mon Sep 17 00:00:00 2001 From: rugk Date: Mon, 17 Dec 2018 18:06:38 +0100 Subject: [PATCH] Fix Firefox for Android compatbility There was an issue in the AddonSettings and FF for Android does not support the "loadReplace" API. --- src/background/modules/MastodonRedirect.js | 16 ++--------- src/background/modules/NetworkTools.js | 33 +++++++++++++++++++--- src/common/modules/AddonSettings | 2 +- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/background/modules/MastodonRedirect.js b/src/background/modules/MastodonRedirect.js index 8a330e8..7ec27fc 100644 --- a/src/background/modules/MastodonRedirect.js +++ b/src/background/modules/MastodonRedirect.js @@ -24,10 +24,7 @@ async function triggerRemoteAction(uri) { // own server if (uri.startsWith(`https://${ownMastodon.server}`)) { // just redirect to given input URL, if it is one the same server - return browser.tabs.update({ - loadReplace: true, - url: uri - }); + return NetworkTools.redirectToWebsite(uri); } const mastodonApiUrl = await Mastodon.getSubscribeApiUrl(ownMastodon, uri); @@ -47,21 +44,14 @@ async function triggerRemoteAction(uri) { // error happened, let's try redirect again without cache // (the API endpoint could have been changed) const mastodonApiUrl = await Mastodon.getSubscribeApiUrl(ownMastodon, uri, true); - const url = (new URL(mastodonApiUrl)).toString(); - browser.tabs.update({ - loadReplace: true, - url: url - }); + NetworkTools.redirectToWebsite(mastodonApiUrl); }; NetworkTools.webRequestListen(url, "onCompleted", verifyRequest); // finally redirect - return browser.tabs.update({ - loadReplace: true, - url: url - }); + return NetworkTools.redirectToWebsite(url); } /** diff --git a/src/background/modules/NetworkTools.js b/src/background/modules/NetworkTools.js index 143f26a..ff28a7c 100644 --- a/src/background/modules/NetworkTools.js +++ b/src/background/modules/NetworkTools.js @@ -7,8 +7,7 @@ /** * Listen to a web request of this URL. * - * @function - * @private + * @public * @param {string} expectedUrl * @param {string} onAction * @param {function} handleWebRequest @@ -26,8 +25,7 @@ export function webRequestListen(expectedUrl, onAction, handleWebRequest, extraI /** * Stops listening to a web request of this URL. * - * @function - * @private + * @public * @param {string} onAction * @param {function} handleWebRequest * @returns {void} @@ -37,3 +35,30 @@ export function webRequestListenStop(onAction, handleWebRequest) { handleWebRequest ); } + +/** + * Redirects the current tab to a new site. + * + * It does try to replace the site's history so the old URL does not appear + * in the history, but this is not always possible. + * + * @public + * @param {string|URL} url + * @returns {Promise} + * @see {@link https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/tabs/update} + */ +export function redirectToWebsite(url) { + if (url instanceof URL) { + url = (new URL(url)).toString(); + } + + // Firefox for Android e.g. does not support "loadReplace" + try { + return browser.tabs.update({ + loadReplace: true, + url: url + }); + } catch (e) { + return browser.tabs.update({ url }); + } +} diff --git a/src/common/modules/AddonSettings b/src/common/modules/AddonSettings index dd342ca..2353f74 160000 --- a/src/common/modules/AddonSettings +++ b/src/common/modules/AddonSettings @@ -1 +1 @@ -Subproject commit dd342cad67ccfac512895f0f40148a2b299bcb0e +Subproject commit 2353f74b7774aa6bf374c2e0efda6ba9306d2b0e