Adaptations pour chrome

This commit is contained in:
Lyanis SOUIDI 2023-03-08 15:31:01 +01:00
parent 148fffd753
commit 8ae749fec5
3 changed files with 149 additions and 143 deletions

View File

@ -1,7 +1,7 @@
function createLink(name, oldURL, newURL) { function createLink(name, oldURL, newURL) {
let e = document.createElement("a"); let e = document.createElement("a");
e.className = "_wims_tester_link"; e.className = "_wims_tester_link";
e.innerText = "[" + browser.i18n.getMessage("openOnServer") + `'${name}']`; e.innerText = "[" + chrome.i18n.getMessage("openOnServer") + `'${name}']`;
let link = new URL(newURL); let link = new URL(newURL);
link.search = new URL(oldURL).search; link.search = new URL(oldURL).search;
e.href = link.toString(); e.href = link.toString();
@ -10,15 +10,15 @@ function createLink(name, oldURL, newURL) {
} }
function updateLink(e, name, url) { function updateLink(e, name, url) {
e.innerText = "[" + browser.i18n.getMessage("openOnServer") + `'${name}']`; e.innerText = "[" + chrome.i18n.getMessage("openOnServer") + `'${name}']`;
let link = new URL(url); let link = new URL(url);
link.search = new URL(e.href).search; link.search = new URL(e.href).search;
e.href = link.toString(); e.href = link.toString();
} }
async function load() { async function load() {
let serversList = await fetch(browser.runtime.getURL("servers.json")).then((response) => response.json()).then((data) => data["servers"]); let serversList = await fetch(chrome.runtime.getURL("servers.json")).then((response) => response.json()).then((data) => data["servers"]);
let preferredServer = await browser.storage.sync.get('preferredServer').then(item => item.preferredServer); let preferredServer = await chrome.storage.sync.get('preferredServer').then(item => item.preferredServer);
if (typeof preferredServer === "undefined" || !preferredServer.hasOwnProperty("url")) preferredServer = serversList[0]; if (typeof preferredServer === "undefined" || !preferredServer.hasOwnProperty("url")) preferredServer = serversList[0];
let list = document.getElementsByClassName("wims_exo_item"); let list = document.getElementsByClassName("wims_exo_item");
@ -37,7 +37,7 @@ document.head.append(style);
load(); load();
browser.storage.sync.onChanged.addListener((changes) => { chrome.storage.sync.onChanged.addListener((changes) => {
const changedItems = Object.keys(changes); const changedItems = Object.keys(changes);
for (const item of changedItems) { for (const item of changedItems) {
if (item == "preferredServer") { if (item == "preferredServer") {

View File

@ -1,17 +1,12 @@
{ {
"manifest_version": 2, "manifest_version": 3,
"name": "WIMS Tester", "name": "WIMS Tester",
"author": "Lyanis Souidi", "author": "Lyanis Souidi",
"version": "1.0", "version": "1.1",
"browser_specific_settings": {
"gecko": {
"id": "wims-tester@lyanis.sh"
}
},
"description": "__MSG_extensionDescription__", "description": "__MSG_extensionDescription__",
"homepage_url": "https://github.com/lyanissouidi/wims-tester", "homepage_url": "https://github.com/lyanissouidi/wims-tester/tree/chrome",
"default_locale": "en", "default_locale": "en",
"browser_action": { "action": {
"default_title": "WIMS Tester", "default_title": "WIMS Tester",
"default_popup": "index.html", "default_popup": "index.html",
"browser_style": true, "browser_style": true,
@ -50,6 +45,12 @@
"js": ["inject.js"] "js": ["inject.js"]
} }
], ],
"web_accessible_resources": [
{
"resources": ["servers.json"],
"matches": ["<all_urls>"]
}
],
"permissions": ["tabs", "storage"], "permissions": ["tabs", "storage"],
"icons": { "icons": {
"16": "icons/16-light.png", "16": "icons/16-light.png",

View File

@ -1,18 +1,22 @@
document.getElementsByTagName("legend")[0].innerText = browser.i18n.getMessage("serverSelection"); document.getElementsByTagName("legend")[0].innerText = chrome.i18n.getMessage("serverSelection");
async function updateLink(name, url) { async function updateLink(name, url) {
let tabURL = await chrome.tabs.query({currentWindow: true, active: true}).then(tabs => tabs[0].url);
if (tabURL.includes("wims.cgi") && !tabURL.includes(url)) {
let link = new URL(url); let link = new URL(url);
let tabURL = await browser.tabs.query({currentWindow: true, active: true}).then(tabs => tabs[0].url);
link.search = new URL(tabURL).search; link.search = new URL(tabURL).search;
let a = document.getElementsByTagName("a")[0]; let a = document.getElementsByTagName("a")[0];
a.href = link.toString(); a.href = link.toString();
a.innerText = browser.i18n.getMessage("openCurrentPageOnServer") + `'${name}'`; a.innerText = chrome.i18n.getMessage("openCurrentPageOnServer") + `'${name}'`;
}
} }
async function load(qualifiedName, value) { async function load() {
let serversList = await fetch(browser.runtime.getURL("servers.json")).then((response) => response.json()).then((data) => data["servers"]); let serversList = await fetch(chrome.runtime.getURL("servers.json")).then((response) => response.json()).then((data) => data["servers"]);
let preferredServer = await browser.storage.sync.get('preferredServer').then(item => item.preferredServer); let preferredServer = await chrome.storage.sync.get('preferredServer').then(item => item.preferredServer);
let tabURL = await chrome.tabs.query({currentWindow: true, active: true}).then(tabs => tabs[0].url);
if (typeof preferredServer === "undefined" || !preferredServer.hasOwnProperty("url")) preferredServer = serversList[0]; if (typeof preferredServer === "undefined" || !preferredServer.hasOwnProperty("url")) preferredServer = serversList[0];
for (let i = 0; i < serversList.length; i++) { for (let i = 0; i < serversList.length; i++) {
if (!tabURL.includes(serversList[i].url)) {
let div = document.createElement("div"); let div = document.createElement("div");
let input = document.createElement("input"); let input = document.createElement("input");
input.type = "radio"; input.type = "radio";
@ -28,10 +32,11 @@ async function load(qualifiedName, value) {
div.appendChild(label); div.appendChild(label);
document.getElementById("servers").appendChild(div); document.getElementById("servers").appendChild(div);
} }
}
updateLink(preferredServer.name, preferredServer.url); updateLink(preferredServer.name, preferredServer.url);
} }
load(); load();
function saveServer(e) { function saveServer(e) {
browser.storage.sync.set({ preferredServer: { name: e.target.value, url: e.target.id } }); chrome.storage.sync.set({ preferredServer: { name: e.target.value, url: e.target.id } });
updateLink(e.target.value, e.target.id); updateLink(e.target.value, e.target.id);
} }