Skip to content

Commit

Permalink
switch to fetch for checking for the test file
Browse files Browse the repository at this point in the history
getLocalFile intentionally doesn't cope with errors, so it was the wrong choice.

This could have been causing real errors on the options page (as `loaded` wasn't being set to true), but I didn't observe any problems.
  • Loading branch information
adam-p committed Oct 17, 2024
1 parent f48a41e commit bdb8177
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/common/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,25 @@ function onLoad() {
// Hide the tests link if the page isn't available. It may be stripped out
// of extension packages.

// Check if our test file exists.
Utils.getLocalFile('./test/index.html', 'text', function(_, err) {
// The test files aren't present, so hide the button.
if (err) {
// Check if our test file exists. Note that we can't use Utils.getLocalFile as it throws
// an asynchronous error if the file isn't found.
// TODO: When Utils.getLocalFile is changed to return a promise, use it here.
fetch('./test/index.html')
.then(response => {
if (!response.ok) {
// The test files aren't present, so hide the button.
$('#tests-link').hide();
}
else {
// When the file is absent, Firefox still gives a 200 status, but will throw an
// error when the response is read.
return response.text();
}
})
.catch(err => {
// The test files aren't present, so hide the button.
$('#tests-link').hide();
}
});
});

// Older Thunderbird may try to open this options page in a new ChromeWindow, and it
// won't work. So in that case we need to tell the user how they can actually open the
Expand Down
5 changes: 4 additions & 1 deletion src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ function getLocalURL(url) {
// Makes an asynchrous XHR request for a local file (basically a thin wrapper).
// `dataType` must be one of 'text', 'json', or 'base64'.
// `callback` will be called with the response value, of a type depending on `dataType`.
// Errors are no expected for local files, and will result in an exception being thrown asynchrously.
// Errors are not expected for local files, and will result in an exception being thrown asynchrously.
// TODO: Return a promise instead of using a callback. This will allow returning an error
// properly, and then this can be used in options.js when checking for the existence of
// the test file.
function getLocalFile(url, dataType, callback) {
fetch(url)
.then(response => {
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "__MSG_app_name__",
"version": "2.14.0",
"version": "2.14.1",
"description": "__MSG_app_slogan__",
"homepage_url": "https://markdown-here.com",
"default_locale": "en",
Expand Down

0 comments on commit bdb8177

Please sign in to comment.