Skip to content

Commit

Permalink
fix: Réactiver le lien vers l'interface Web de Kodi.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Aug 13, 2021
1 parent 3cc8497 commit 6d6a90d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/popup/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { cast, kodi } from "../core/index.js";
import { complete } from "../core/labellers.js";
import { notify } from "../core/notify.js";
import { ping } from "../tools/ping.js";

/**
* La position de l'élément courant dans la liste de lecture ; ou
Expand Down Expand Up @@ -859,6 +860,14 @@ const load = async function () {
document.querySelector("#feedback").disabled = false;
document.querySelector("#donate").disabled = false;
document.querySelector("#rate").disabled = false;

// Afficher le bouton vers l'interface Web de Kodi seulement si
// celle-ci est accessible.
const url = `http://${kodi.url.hostname}:8080`;
if (await ping(url)) {
document.querySelector("#web").dataset.url = url;
document.querySelector("#web").style.display = "block";
}
} catch (err) {
splash(err);
}
Expand Down
27 changes: 27 additions & 0 deletions src/tools/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @module
*/

/**
* Teste si un lien est accessible.
*
* @param {string} url Le lien testé.
* @returns {Promise<boolean>} Une promesse indiquant si le lien est accessible.
*/
export const ping = async function (url) {
try {
await fetch(url, {
method: "HEAD",
// Fournir des "identifiants" vides dans les entêtes pour que si la
// page demande une authentification : celle-ci échoue directement
// sans demander à l'utilisateur de saisir son identifiant et son
// mot de passe.
headers: { Authorization: "" },
});
return true;
} catch {
// Ignorer l'erreur si la requête échoue. Et indiquer que le lien est
// inaccessible.
return false;
}
};
37 changes: 37 additions & 0 deletions test/unit/tools/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from "node:assert";
import sinon from "sinon";
import { ping } from "../../../src/tools/ping.js";

describe("tools/ping.js", function () {
describe("ping()", function () {
it("should return true", async function () {
const stub = sinon.stub(globalThis, "fetch").resolves();

const ok = await ping("http://foo.com/");
assert.strictEqual(ok, true);

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["http://foo.com/", {
method: "HEAD",
headers: { Authorization: "" },
}]);

stub.restore();
});

it("should return false", async function () {
const stub = sinon.stub(globalThis, "fetch").rejects();

const ok = await ping("http://foo.com/");
assert.strictEqual(ok, false);

assert.strictEqual(stub.callCount, 1);
assert.deepStrictEqual(stub.firstCall.args, ["http://foo.com/", {
method: "HEAD",
headers: { Authorization: "" },
}]);

stub.restore();
});
});
});

0 comments on commit 6d6a90d

Please sign in to comment.