From f65cf03d946b63a32b438affff1fa705fc400f91 Mon Sep 17 00:00:00 2001 From: nitatemic Date: Sun, 12 Jun 2022 23:06:55 +0200 Subject: [PATCH 1/7] Change folders and add the base of the extension --- .eslintrc | 4 +- .gitignore | 208 ++++++++++++++++++ js/background.js | 6 + js/popup.js | 0 .../replikaWithDeepl.js | 0 manifest.json | 10 + popup.html | 19 ++ 7 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 js/background.js create mode 100644 js/popup.js rename replikaWithDeepl.js => legacy/replikaWithDeepl.js (100%) create mode 100644 manifest.json create mode 100644 popup.html diff --git a/.eslintrc b/.eslintrc index afcc776..5ea4e0e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,10 +1,10 @@ { "env": { - "node": true + "browser": true, + "es2021": true }, "extends": "airbnb-base", "parserOptions": { - "ecmaVersion": 6, "sourceType": "module" } } diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1041dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,208 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser diff --git a/js/background.js b/js/background.js new file mode 100644 index 0000000..b000df8 --- /dev/null +++ b/js/background.js @@ -0,0 +1,6 @@ +/* Lorsque l'extension Chrome est installé, ouvre la documentation de l'extension */ +chrome.runtime.onInstalled.addListener(function() { + chrome.tabs.create({ + url: "https://github.com/nitatemic/ReplikaWithDeepL/wiki/You-just-installed-the-extension-and-now%3F" + }); +} diff --git a/js/popup.js b/js/popup.js new file mode 100644 index 0000000..e69de29 diff --git a/replikaWithDeepl.js b/legacy/replikaWithDeepl.js similarity index 100% rename from replikaWithDeepl.js rename to legacy/replikaWithDeepl.js diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..502768d --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Replika with DeepL", + "description": "Talk with your Replika in your native language", + "version": "1.0", + "manifest_version": 3, + "background": { + "service_worker": "background.js" + }, + "permissions": ["storage"] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..3400560 --- /dev/null +++ b/popup.html @@ -0,0 +1,19 @@ + + + + + Replika with DeepL + + +

Your DeepL API key

+ +

Mode

+ + + + From f7fa10c983ed49448d9e3772da5b5763124155bf Mon Sep 17 00:00:00 2001 From: nitatemic Date: Mon, 13 Jun 2022 01:31:29 +0200 Subject: [PATCH 2/7] Split code and add test --- js/background.js | 6 ++- js/popup.js | 4 ++ js/popup/__tests__/getAPIKEY.test.js | 14 +++++++ js/popup/__tests__/sendTestRequest.test.js | 8 ++++ js/popup/__tests__/testAPIKEY.test.js | 8 ++++ js/popup/chooseEndpoint.js | 14 +++++++ js/popup/getAPIKEY.js | 10 +++++ js/popup/sendTestRequest.js | 26 ++++++++++++ js/popup/testAPIKEY.js | 49 ++++++++++++++++++++++ legacy/DeepL API test.http | 0 manifest.json | 7 +++- popup.html | 5 ++- 12 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 js/popup/__tests__/getAPIKEY.test.js create mode 100644 js/popup/__tests__/sendTestRequest.test.js create mode 100644 js/popup/__tests__/testAPIKEY.test.js create mode 100644 js/popup/chooseEndpoint.js create mode 100644 js/popup/getAPIKEY.js create mode 100644 js/popup/sendTestRequest.js create mode 100644 js/popup/testAPIKEY.js create mode 100644 legacy/DeepL API test.http diff --git a/js/background.js b/js/background.js index b000df8..26ab34b 100644 --- a/js/background.js +++ b/js/background.js @@ -3,4 +3,8 @@ chrome.runtime.onInstalled.addListener(function() { chrome.tabs.create({ url: "https://github.com/nitatemic/ReplikaWithDeepL/wiki/You-just-installed-the-extension-and-now%3F" }); -} + /* Définir l'endpoint par défaut */ + chrome.storage.sync.set({ + "endpoint": "https://api.deepl.com/v2" + }); +}); diff --git a/js/popup.js b/js/popup.js index e69de29..eaf3abc 100644 --- a/js/popup.js +++ b/js/popup.js @@ -0,0 +1,4 @@ +const ChooseEndpoint = require("./popup/chooseEndpoint"); +const getAPIKEY = require("./popup/getAPIKEY"); +const testAPIKEY = require("./popup/testAPIKEY"); +const sendTestRequest = require("./popup/sendTestRequest"); diff --git a/js/popup/__tests__/getAPIKEY.test.js b/js/popup/__tests__/getAPIKEY.test.js new file mode 100644 index 0000000..d801b44 --- /dev/null +++ b/js/popup/__tests__/getAPIKEY.test.js @@ -0,0 +1,14 @@ +require('dotenv/config'); +const getAPIKEY = require('../getAPIKEY'); +const API_KEY_TEST = process.env.API_KEY_TEST; + +test(`Return the API key`, () => { + /* Set the API key in the input field */ + document.body.innerHTML = + `
+ +
`; + + /* Check the value of the API key */ + expect(getAPIKEY()).toBe(API_KEY_TEST); + }); diff --git a/js/popup/__tests__/sendTestRequest.test.js b/js/popup/__tests__/sendTestRequest.test.js new file mode 100644 index 0000000..8876243 --- /dev/null +++ b/js/popup/__tests__/sendTestRequest.test.js @@ -0,0 +1,8 @@ +require('dotenv/config'); +const sendTestRequest = require('../sendTestRequest'); +const API_KEY_TEST = process.env.API_KEY_TEST; + +test(`Error Handling in sendTestRequest`, () => { + /* Check the value of the API key */ + expect(sendTestRequest()).toEqual( "Fetch error"); +}); diff --git a/js/popup/__tests__/testAPIKEY.test.js b/js/popup/__tests__/testAPIKEY.test.js new file mode 100644 index 0000000..8f00147 --- /dev/null +++ b/js/popup/__tests__/testAPIKEY.test.js @@ -0,0 +1,8 @@ +require('dotenv/config'); +const testAPIKEY = require('../testAPIKEY'); +const API_KEY_TEST = process.env.API_KEY_TEST; + +test(`Error Handling in testAPIKEY`, () => { + /* Check the value of the API key */ + expect(testAPIKEY()).toEqual({"message": "Fetch error", "status": "error"}); +}); diff --git a/js/popup/chooseEndpoint.js b/js/popup/chooseEndpoint.js new file mode 100644 index 0000000..ebd9230 --- /dev/null +++ b/js/popup/chooseEndpoint.js @@ -0,0 +1,14 @@ +const sendTestRequest = require("./sendTestRequest"); + +function ChooseEndpoint(APIKEY) { + let request = sendTestRequest(APIKEY); + /* Parsing the response from the server into a JavaScript object. */ + let response = JSON.parse(request.response); + if (response.message === "Wrong endpoint. Use https://api-free.deepl.com") { + /*Enregistre l'endpoint dans le storage de l'extension*/ + chrome.storage.sync.set({ + "endpoint": "https://api-free.deepl.com/v2" + }); + } +} +module.exports = ChooseEndpoint; diff --git a/js/popup/getAPIKEY.js b/js/popup/getAPIKEY.js new file mode 100644 index 0000000..82a2502 --- /dev/null +++ b/js/popup/getAPIKEY.js @@ -0,0 +1,10 @@ +/** + * The function `getAPIKEY()` is called when the user clicks the "Submit" button. The function then + * takes the value of the text box and assigns it to the variable `API_KEY` + */ + +function getAPIKEY() { + return document.getElementById("API_KEY").value; +} + +module.exports = getAPIKEY; diff --git a/js/popup/sendTestRequest.js b/js/popup/sendTestRequest.js new file mode 100644 index 0000000..514a3fd --- /dev/null +++ b/js/popup/sendTestRequest.js @@ -0,0 +1,26 @@ +function sendTestRequest(APIKEY) { + /* Get endpoint from storage */ + let endpoint; + try { + chrome.storage.sync.get("endpoint", function(result) { + endpoint = result.endpoint; + }); + } + catch (e) { + endpoint = "https://api.deepl.com/v2"; + } + + /* Faire une requête à DeepL pour vérifier que l'API_KEY est correcte */ + try { + fetch(`${endpoint}usage?auth_key=${APIKEY}`) + .then(response => { + return response + }); + } + catch (e) { + return 'Fetch error'; + } + +} + +module.exports = sendTestRequest; diff --git a/js/popup/testAPIKEY.js b/js/popup/testAPIKEY.js new file mode 100644 index 0000000..bd02a68 --- /dev/null +++ b/js/popup/testAPIKEY.js @@ -0,0 +1,49 @@ +const sendTestRequest = require("./sendTestRequest"); +function testAPIKEY(API_KEY) { + + if (API_KEY === "") { + return { + "status": "error", + "message": "Please enter your API key" + }; + } + + let request = sendTestRequest(API_KEY); + if (request === "Fetch error") { + return { + "status": "error", + "message": "Fetch error" + }; + } + /* Parsing the response from the server into a JavaScript object. */ + let response = JSON.parse(request.body); + + switch (request.status) { + case 200: + return { + "status": "success", + "message": "Your API key is valid" + } + + case 456: + return { + "status": "error", + "message": "Your API key is valid but you have reached the limit of your account" + } + + case 500: + return { + "status": "error", + "message": "DeepL seems to be down. Please try again later" + } + + default: + console.error(response.message); + return { + "status": "error", + "message": "Check the console for more information" + } + } +} + +module.exports = testAPIKEY; diff --git a/legacy/DeepL API test.http b/legacy/DeepL API test.http new file mode 100644 index 0000000..e69de29 diff --git a/manifest.json b/manifest.json index 502768d..a27773f 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,10 @@ "version": "1.0", "manifest_version": 3, "background": { - "service_worker": "background.js" + "service_worker": "./js/background.js" }, - "permissions": ["storage"] + "permissions": ["storage"], + "action": { + "default_popup": "popup.html" + } } diff --git a/popup.html b/popup.html index 3400560..9df8725 100644 --- a/popup.html +++ b/popup.html @@ -7,13 +7,16 @@

Your DeepL API key

Mode

+