Skip to content

Commit

Permalink
Fix Firefox for Android compatbility
Browse files Browse the repository at this point in the history
There was an issue in the AddonSettings and FF for Android does not support the "loadReplace" API.
  • Loading branch information
rugk committed Dec 17, 2018
1 parent d12a9ad commit 4b4c978
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
16 changes: 3 additions & 13 deletions src/background/modules/MastodonRedirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
33 changes: 29 additions & 4 deletions src/background/modules/NetworkTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
/**
* Listen to a web request of this URL.
*
* @function
* @private
* @public
* @param {string} expectedUrl
* @param {string} onAction
* @param {function} handleWebRequest
Expand All @@ -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}
Expand All @@ -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 });
}
}
2 changes: 1 addition & 1 deletion src/common/modules/AddonSettings
Submodule AddonSettings updated 1 files
+45 −31 AddonSettings.js

0 comments on commit 4b4c978

Please sign in to comment.