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,47 +1,47 @@
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();
e.target = "_blank"; e.target = "_blank";
return e; return e;
} }
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");
for (let item of list) item.firstElementChild.after(createLink(preferredServer.name, item.firstElementChild.href, preferredServer.url)); for (let item of list) item.firstElementChild.after(createLink(preferredServer.name, item.firstElementChild.href, preferredServer.url));
list = document.querySelectorAll('a[target="wims_exo"]'); list = document.querySelectorAll('a[target="wims_exo"]');
for (let item of list) item.after(createLink(preferredServer.name, item.href, preferredServer.url)); for (let item of list) item.after(createLink(preferredServer.name, item.href, preferredServer.url));
list = document.querySelectorAll('ul[class="wims_home_result_list"] > li'); list = document.querySelectorAll('ul[class="wims_home_result_list"] > li');
for (let item of list) item.firstElementChild.after(createLink(preferredServer.name, item.firstElementChild.href, preferredServer.url)); for (let item of list) item.firstElementChild.after(createLink(preferredServer.name, item.firstElementChild.href, preferredServer.url));
} }
let style = document.createElement('style'); let style = document.createElement('style');
style.textContent = "._wims_tester_link::before { content: ' '; } ._wims_tester_link { font-size: small; color: black; }"; style.textContent = "._wims_tester_link::before { content: ' '; } ._wims_tester_link { font-size: small; color: black; }";
document.head.append(style); 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") {
document.querySelectorAll('a[class="_wims_tester_link"]').forEach(e => updateLink(e, changes[item].newValue.name, changes[item].newValue.url)); document.querySelectorAll('a[class="_wims_tester_link"]').forEach(e => updateLink(e, changes[item].newValue.name, changes[item].newValue.url));
} }
} }
}); });

View File

@ -1,61 +1,62 @@
{ {
"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": { "description": "__MSG_extensionDescription__",
"gecko": { "homepage_url": "https://github.com/lyanissouidi/wims-tester/tree/chrome",
"id": "wims-tester@lyanis.sh" "default_locale": "en",
} "action": {
}, "default_title": "WIMS Tester",
"description": "__MSG_extensionDescription__", "default_popup": "index.html",
"homepage_url": "https://github.com/lyanissouidi/wims-tester", "browser_style": true,
"default_locale": "en", "default_icon": {
"browser_action": { "16": "icons/16-light.png",
"default_title": "WIMS Tester", "32": "icons/32-light.png",
"default_popup": "index.html", "48": "icons/48-light.png",
"browser_style": true, "96": "icons/96-light.png",
"default_icon": { "128": "icons/128-light.png"
"16": "icons/16-light.png", },
"32": "icons/32-light.png", "theme_icons": [{
"48": "icons/48-light.png", "light": "icons/16-light.png",
"96": "icons/96-light.png", "dark": "icons/16-dark.png",
"128": "icons/128-light.png" "size": 16
}, }, {
"theme_icons": [{ "light": "icons/32-light.png",
"light": "icons/16-light.png", "dark": "icons/32-dark.png",
"dark": "icons/16-dark.png", "size": 32
"size": 16 }, {
}, { "light": "icons/48-light.png",
"light": "icons/32-light.png", "dark": "icons/48-dark.png",
"dark": "icons/32-dark.png", "size": 48
"size": 32 }, {
}, { "light": "icons/96-light.png",
"light": "icons/48-light.png", "dark": "icons/96-dark.png",
"dark": "icons/48-dark.png", "size": 96
"size": 48 }, {
}, { "light": "icons/128-light.png",
"light": "icons/96-light.png", "dark": "icons/128-dark.png",
"dark": "icons/96-dark.png", "size": 128
"size": 96 }]
}, { },
"light": "icons/128-light.png", "content_scripts": [
"dark": "icons/128-dark.png", {
"size": 128 "matches": ["*://*/*wims.cgi*"],
}] "js": ["inject.js"]
}, }
"content_scripts": [ ],
{ "web_accessible_resources": [
"matches": ["*://*/*wims.cgi*"], {
"js": ["inject.js"] "resources": ["servers.json"],
} "matches": ["<all_urls>"]
], }
"permissions": ["tabs", "storage"], ],
"icons": { "permissions": ["tabs", "storage"],
"16": "icons/16-light.png", "icons": {
"32": "icons/32-light.png", "16": "icons/16-light.png",
"48": "icons/48-light.png", "32": "icons/32-light.png",
"96": "icons/96-light.png", "48": "icons/48-light.png",
"128": "icons/128-light.png" "96": "icons/96-light.png",
} "128": "icons/128-light.png"
}
} }

View File

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