From 28c1ebbacf50a7bf79008136285c7261a517365d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C3=BBki=20Vachot?= Date: Sun, 4 Sep 2022 18:39:13 +0200 Subject: [PATCH] Add files via upload --- background.js | 10 +++++++++ dofusbook.atelier.copy.js | 46 +++++++++++++++++++++++++++------------ manifest.json | 25 ++++++++++++++------- 3 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 background.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..a00aaca --- /dev/null +++ b/background.js @@ -0,0 +1,10 @@ +chrome.runtime.onInstalled.addListener(function() { + chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { + if(changeInfo.status == 'complete' && tab.url == 'https://www.dofusbook.net/fr/outils/atelier' && tab.status == 'complete'){ + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ["dofusbook.atelier.copy.js"] + }); + } + }); +}); diff --git a/dofusbook.atelier.copy.js b/dofusbook.atelier.copy.js index 226e2f7..307ba5b 100644 --- a/dofusbook.atelier.copy.js +++ b/dofusbook.atelier.copy.js @@ -1,26 +1,44 @@ -function waitResources() { +function addCopyButtonAtelier() { + + const buttonPropertiesConstants = { + "textButtonCopy": "copy", + "backgroundColorButtonCopy": "", + "textButtonCopied": "copied", + "backgroundColorButtonCopied": "#6dc700" + }; + const itemCells = document.getElementsByClassName('cell'); - + if(itemCells.length > 0){ for(const itemCell of itemCells) { const itemWrapper = itemCell.getElementsByClassName('wrapper').item(0); const itemName = itemWrapper.getElementsByClassName('label').item(0).innerHTML.slice(1, -1); - let itemCopyButton = document.createElement("button"); - itemCopyButton.innerHTML = "copy"; - itemCopyButton.onclick = function(){ - navigator.clipboard.writeText(itemName).then(() => { - console.info(`${itemName} copied !`); - }, () => { - console.error(`ERROR ${itemName} not copied !`); - }); - }; + if(itemWrapper.getElementsByClassName('copy-btn').length == 0){ + let itemCopyButton = document.createElement("button"); + itemCopyButton.innerHTML = buttonPropertiesConstants.textButtonCopy; + itemCopyButton.setAttribute('class', 'copy-btn'); + + itemCopyButton.onclick = function(){ + navigator.clipboard.writeText(itemName).then(() => { + console.info(`${itemName} copied !`); + itemCopyButton.innerHTML = buttonPropertiesConstants.textButtonCopied; + itemCopyButton.style.backgroundColor = buttonPropertiesConstants.backgroundColorButtonCopied; + setTimeout(function(){ + itemCopyButton.innerHTML = buttonPropertiesConstants.textButtonCopy; + itemCopyButton.style.backgroundColor = buttonPropertiesConstants.backgroundColorButtonCopy; + }, 3000); + }, () => { + console.error(`ERROR ${itemName} not copied !`); + }); + }; - itemWrapper.appendChild(itemCopyButton); + itemWrapper.appendChild(itemCopyButton); + } else { console.error("copy-btn already exist"); } } } else { - setTimeout(waitResources, 250); // try again in 300 milliseconds + setTimeout(addCopyButtonAtelier, 250); // try again in 300 milliseconds } } -waitResources(); \ No newline at end of file +addCopyButtonAtelier(); \ No newline at end of file diff --git a/manifest.json b/manifest.json index c8a8e12..afc1080 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,22 @@ { "name": "DofusBook Atelier Bouton Copier", "description": "Ajout d'un bouton pour copier chaque nom de ressource dans l'atelier de DofusBook", - "version": "1.0", + "version": "1.1.0", "manifest_version": 3, - "content_scripts": [ - { - "matches": ["https://www.dofusbook.net/fr/outils/atelier"], - "js": ["dofusbook.atelier.copy.js"], - "run_at": "document_end" - } + "icons": { + "16": "./icons/icon16.png", + "32": "./icons/icon32.png", + "48": "./icons/icon48.png", + "128": "./icons/icon128.png" + }, + "permissions": [ + "tabs", + "scripting" + ], + "background": { + "service_worker": "background.js" + }, + "host_permissions": [ + "https://www.dofusbook.net/*" ] -} +} \ No newline at end of file