From 1f432fbfe924e24fa1bd2ebbb604521558df19c0 Mon Sep 17 00:00:00 2001 From: Joabesv Date: Tue, 13 Feb 2024 02:08:21 -0300 Subject: [PATCH 1/6] chore: remove unused code --- src/scripts/contentscript.js | 9 +-------- src/studentPortal.js | 14 -------------- src/utils/convertUfabcDisciplina.js | 1 - src/utils/extensionUtils.js | 5 ----- src/views/Matricula.vue | 14 -------------- src/views/Portal.vue | 9 --------- 6 files changed, 1 insertion(+), 51 deletions(-) delete mode 100644 src/studentPortal.js delete mode 100644 src/views/Portal.vue diff --git a/src/scripts/contentscript.js b/src/scripts/contentscript.js index 5151fee..751a409 100644 --- a/src/scripts/contentscript.js +++ b/src/scripts/contentscript.js @@ -1,11 +1,3 @@ -// https://crx.dam.io/ext/gphjopenfpnlnffmhhhhdiecgdcopmhk.html -// add extension id to window -const isBrowser = typeof chrome != "undefined" && !!chrome.storage; -// var script = document.createElement('script'); -// const extension_id = isBrowser ? chrome.i18n.getMessage("@@extension_id") : null; -// script.innerHTML = `extension_id = "${extension_id}"`; -// (document.head || document.documentElement).appendChild(script) - import $ from "jquery"; import _ from "lodash"; @@ -17,6 +9,7 @@ import Utils from "../utils/extensionUtils"; // CSS imports import "element-ui/lib/theme-chalk/index.css"; +const isBrowser = typeof chrome != "undefined" && !!chrome.storage; let matricula_url; if (process.env.NODE_ENV == "production") { diff --git a/src/studentPortal.js b/src/studentPortal.js deleted file mode 100644 index 435907c..0000000 --- a/src/studentPortal.js +++ /dev/null @@ -1,14 +0,0 @@ -import Vue from "vue"; -import Vuetify from "vuetify"; - -Vue.use(Vuetify); - -import Portal from "./views/Portal.vue"; - -new Vue({ - el: "#app", - data: { - name: "portal-matricula-extension", - }, - render: (h) => h(Portal), -}); diff --git a/src/utils/convertUfabcDisciplina.js b/src/utils/convertUfabcDisciplina.js index 4284909..23c3933 100644 --- a/src/utils/convertUfabcDisciplina.js +++ b/src/utils/convertUfabcDisciplina.js @@ -71,7 +71,6 @@ export function convertDisciplina(d) { // fix disciplina obj.disciplina = disciplina.join(" ").trim(); - //obj.ideal_quad = app.helpers.season.findIdeais().includes(obj.codigo) obj.disciplina_id = obj.id; obj.codigo = obj.codigo; diff --git a/src/utils/extensionUtils.js b/src/utils/extensionUtils.js index 0657e3d..7adee5b 100644 --- a/src/utils/extensionUtils.js +++ b/src/utils/extensionUtils.js @@ -4,11 +4,6 @@ import is from "is_js"; import _ from "lodash"; function ExtensionUtils() { - // force initialization of xdLocalStorage - // window.xdLocalStorage.init({ - // iframeUrl: getExtensionUrl("pages/iframe.html"), - // }); - const IS_BROWSER = typeof chrome != "undefined" && !!chrome.storage; const EXTENSION_ID = IS_BROWSER ? chrome.i18n.getMessage("@@extension_id") diff --git a/src/views/Matricula.vue b/src/views/Matricula.vue index d453868..acd964e 100644 --- a/src/views/Matricula.vue +++ b/src/views/Matricula.vue @@ -93,7 +93,6 @@ import $ from "jquery"; import matriculaUtils from "../utils/Matricula"; import Utils from "../utils/extensionUtils"; import Mustache from "mustache"; -// await MatriculaHelper.getTotalMatriculas() export default { name: "App", @@ -228,19 +227,6 @@ export default { } }); }); - - // chrome.runtime.sendMessage(Utils.EXTENSION_ID, { - // method: 'storage', - // key: - // }, function(item) { - // if (item == null) { - // self.$notify({ - // message: 'Não temos as diciplinas que você cursou, acesse o Portal do Aluno' - // }) - // return - // } - - // }) }, changeTeachers() { let self = this; diff --git a/src/views/Portal.vue b/src/views/Portal.vue deleted file mode 100644 index bd204f9..0000000 --- a/src/views/Portal.vue +++ /dev/null @@ -1,9 +0,0 @@ - - - From 4585a850568528f248417b3f6582227064ea4397 Mon Sep 17 00:00:00 2001 From: Joabesv Date: Tue, 13 Feb 2024 02:37:16 -0300 Subject: [PATCH 2/6] chore: use utils classes and storage service --- src/components/ReviewTeacher.vue | 4 +- src/lib/init.cjs | 4 +- src/scripts/contentScriptPortal.js | 21 +++-- src/scripts/contentscript.js | 22 ++--- src/utils/Matricula.js | 6 +- src/utils/extensionUtils.js | 146 +++++++---------------------- src/views/Matricula.vue | 67 +++++++------ 7 files changed, 96 insertions(+), 174 deletions(-) diff --git a/src/components/ReviewTeacher.vue b/src/components/ReviewTeacher.vue index bc3ba9c..56ce586 100644 --- a/src/components/ReviewTeacher.vue +++ b/src/components/ReviewTeacher.vue @@ -107,7 +107,7 @@ import Highcharts from "highcharts"; import _ from "lodash"; import { NextAPI } from "../services/NextAPI"; -import Utils from "../utils/extensionUtils"; +import { NextStorage } from "../services/NextStorage"; import matriculaUtils from "../utils/Matricula"; Highcharts3D(Highcharts); @@ -335,7 +335,7 @@ export default { let self = this; const storageUser = "ufabc-extension-" + matriculaUtils.currentUser(); - Utils.storage.getItem(storageUser).then((item) => { + NextStorage.getItem(storageUser).then((item) => { if (item == null) return; self.student_cr = _.get(item, "[1].cr", 0) || _.get(item, "[0].cr", 0); }); diff --git a/src/lib/init.cjs b/src/lib/init.cjs index 08eab28..1d6267c 100644 --- a/src/lib/init.cjs +++ b/src/lib/init.cjs @@ -1,8 +1,8 @@ -import Utils from "../utils/extensionUtils"; +import { extensionUtils } from "../utils/extensionUtils"; xdLocalStorage.init({ /* required */ - iframeUrl: Utils.getExtensionUrl("pages/iframe.html"), + iframeUrl: extensionUtils.extensionURL("pages/iframe.html"), //an option function to be called right after the iframe was loaded and ready for action initCallback: function () { console.log("Got iframe ready"); diff --git a/src/scripts/contentScriptPortal.js b/src/scripts/contentScriptPortal.js index e4337fe..7c54da4 100644 --- a/src/scripts/contentScriptPortal.js +++ b/src/scripts/contentScriptPortal.js @@ -1,8 +1,9 @@ import toastr from "toastr"; import $ from "jquery"; import _ from "lodash"; -import Utils from "../utils/extensionUtils"; +import { extensionUtils } from "../utils/extensionUtils"; import { NextAPI } from "../services/NextAPI"; +import { NextStorage } from "../services/NextStorage"; import Axios from "axios"; import Toastify from "toastify-js"; @@ -43,19 +44,19 @@ if (isPortalAluno) { anchor.setAttribute("id", "app"); document.body.append(anchor); - Utils.injectStyle("styles/portal.css"); + extensionUtils.injectStyle("styles/portal.css"); toastr.info( "Clique em Ficha Individual para atualizar suas informações!" ); } else if (isFichasIndividuais) { - Utils.injectStyle("styles/portal.css"); + extensionUtils.injectStyle("styles/portal.css"); toast.showToast(); iterateTabelaCursosAndSaveToLocalStorage(); } else if (isStudentFicha) { - Utils.injectStyle("styles/portal.css"); + extensionUtils.injectStyle("styles/portal.css"); } function iterateTabelaCursosAndSaveToLocalStorage() { @@ -101,7 +102,7 @@ async function getFichaAluno(fichaAlunoUrl, nomeDoCurso, anoDaGrade) { )[1] || "some ra"; const storageRA = "ufabc-extension-ra-" + getEmailAluno(); - await Utils.storage.setItem(storageRA, ra); + await NextStorage.setItem(storageRA, ra); const jsonFicha = await Axios.get( "https://aluno.ufabc.edu.br" + fichaAlunoUrl, @@ -203,13 +204,13 @@ function toNumber(el) { async function saveToLocalStorage(curso) { const storageUser = "ufabc-extension-" + getEmailAluno(); - let user = await Utils.storage.getItem(storageUser); + let user = await NextStorage.getItem(storageUser); if (!user || _.isEmpty(user)) user = []; user.push(curso); user = _.uniqBy(user, "curso"); - await Utils.storage.setItem(storageUser, user); + await NextStorage.setItem(storageUser, user); toastr.success( `Suas informações foram salvas! Disciplinas do curso do ${curso.curso} @@ -221,11 +222,11 @@ async function saveToLocalStorage(curso) { async function saveStudentsToLocalStorage(curso) { const storageUser = "ufabc-extension-" + getEmailAluno(); - const cursos = await Utils.storage.getItem(storageUser); + const cursos = await NextStorage.getItem(storageUser); const ra = (curso && curso.ra) || null; let allSavedStudents = []; - const students = await Utils.storage.getItem("ufabc-extension-students"); + const students = await NextStorage.getItem("ufabc-extension-students"); if (students && students.length) { allSavedStudents.push(...students); } @@ -238,5 +239,5 @@ async function saveStudentsToLocalStorage(curso) { lastUpdate: Date.now(), }; allSavedStudents.unshift(student); - await Utils.storage.setItem("ufabc-extension-students", allSavedStudents); + await NextStorage.setItem("ufabc-extension-students", allSavedStudents); } diff --git a/src/scripts/contentscript.js b/src/scripts/contentscript.js index 751a409..6c136a8 100644 --- a/src/scripts/contentscript.js +++ b/src/scripts/contentscript.js @@ -1,13 +1,13 @@ +// CSS imports +import "element-ui/lib/theme-chalk/index.css"; + import $ from "jquery"; import _ from "lodash"; import matriculaUtils from "../utils/Matricula"; import { setupStorage } from "../utils/setupStorage"; - -import Utils from "../utils/extensionUtils"; - -// CSS imports -import "element-ui/lib/theme-chalk/index.css"; +import { extensionUtils } from "../utils/extensionUtils"; +import { NextStorage } from "../services/NextStorage"; const isBrowser = typeof chrome != "undefined" && !!chrome.storage; let matricula_url; @@ -42,9 +42,9 @@ if (!isBrowser) { async function load() { const currentUrl = document.location.href; // add cross-domain local storage - Utils.injectScript("lib/xdLocalStorage.min.js"); - Utils.injectIframe("pages/iframe.html"); - Utils.injectScript("lib/init.js"); + extensionUtils.injectScript("lib/xdLocalStorage.min.js"); + extensionUtils.injectIframe("pages/iframe.html"); + extensionUtils.injectScript("lib/init.js"); setupStorage(); await import("./contentScriptPortal"); @@ -54,7 +54,7 @@ async function load() { setTimeout(async () => { let lastUpdate = null; try { - lastUpdate = await Utils.storage.getItem("ufabc-extension-last"); + lastUpdate = await NextStorage.getItem("ufabc-extension-last"); } catch (err) { lastUpdate = Date.now(); } finally { @@ -68,7 +68,7 @@ async function load() { $("#meio").prepend(anchor); //inject styles - Utils.injectStyle("styles/main.css"); + extensionUtils.injectStyle("styles/main.css"); // manda as informacoes para o servidor matriculaUtils.sendAlunoData(); @@ -92,7 +92,7 @@ async function load() { document.body.append(reviewSubject); // inject Vue app - Utils.injectScript("scripts/main.js"); + extensionUtils.injectScript("scripts/main.js"); }, 1500); } } diff --git a/src/utils/Matricula.js b/src/utils/Matricula.js index 94ac196..1a1f27c 100644 --- a/src/utils/Matricula.js +++ b/src/utils/Matricula.js @@ -3,7 +3,7 @@ import { NextAPI } from "../services/NextAPI"; import { toJSON } from "./toJSON"; import _ from "lodash"; import $ from "jquery"; -import Utils from "./extensionUtils"; +import { NextStorage } from "../services/NextStorage"; const nextApi = NextAPI(); @@ -32,8 +32,8 @@ function Matricula() { async function getProfessors() { try { let { data: professors } = await nextApi.get("/disciplinas"); - await Utils.storage.setItem("ufabc-extension-last", Date.now()); - await Utils.storage.setItem("ufabc-extension-disciplinas", professors); + await NextStorage.setItem("ufabc-extension-last", Date.now()); + await NextStorage.setItem("ufabc-extension-disciplinas", professors); return professors; } catch (e) { console.log("❌ Erro ao atualizar disciplinas"); diff --git a/src/utils/extensionUtils.js b/src/utils/extensionUtils.js index 7adee5b..f4c97f1 100644 --- a/src/utils/extensionUtils.js +++ b/src/utils/extensionUtils.js @@ -1,128 +1,50 @@ -import $ from "jquery"; import Axios from "axios"; import is from "is_js"; -import _ from "lodash"; -function ExtensionUtils() { - const IS_BROWSER = typeof chrome != "undefined" && !!chrome.storage; - const EXTENSION_ID = IS_BROWSER - ? chrome.i18n.getMessage("@@extension_id") - : null; +class ExtensionUtils { + IS_BROWSER = typeof chrome !== "undefined" && !!chrome.storage; + #EXTENSION_ID = IS_BROWSER ? chrome.i18n.getMessage("@@extension_id") : null; - var getChromeUrl = function (url) { - return getExtensionUrl(url); - }; - - var fetchChromeUrl = async function (url, cb) { - return Axios.get(getChromeUrl(url)); - }; - - var injectDiv = async function (link, el) { - let resp = await fetchChromeUrl(link); - const data = resp.data; - - var div = document.createElement("div"); - div.innerHTML = data; - - if (el) { - let parent = el.parentNode; - parent.insertBefore(div, el.nextSibling); + extensionURL(link) { + if (this.#EXTENSION_ID) { + const prefix = is.chrome() ? "chrome-extension://" : "moz-extension://"; + return `${prefix}${EXTENSION_ID}/${link.replace(/^\//, "")}`; } else { - document.body.appendChild(div); + // fallback in case we cant detect the browser + return `https://next-extension.captain.sv.ufabcnext.com/static/${link}`; } - }; - - var injectStyle = function (link) { - var s = document.createElement("link"); - s.href = getExtensionUrl(link); - s.type = "text/css"; - s.rel = "stylesheet"; + } - document.head.appendChild(s); - }; + chromeURL(url) { + return this.extensionURL(url); + } - var injectScript = function (link) { - var s = document.createElement("script"); - s.src = getExtensionUrl(link); + fetchChromeURL(url) { + return Axios.get(this.chromeURL(url)); + } - (document.head || document.documentElement).appendChild(s); - }; + injectStyle(link) { + const styleTag = document.createElement("link"); + styleTag.href = this.extensionURL(link); + styleTag.type = "text/css"; + styleTag.rel = "stylesheet"; - var injectIframe = function (link) { - var s = document.createElement("iframe"); - s.src = getExtensionUrl(link); - s.setAttribute("style", "display: none;"); - (document.body || document.documentElement).appendChild(s); - }; + document.head.appendChild(injectStyle); + } - var storage = { - setItem(key, value) { - return new Promise((resolve, reject) => { - try { - const date = Date.now(); - const event = new CustomEvent("requestStorage", { - detail: { - method: `setStorage-${key}-${date}`, - date: date, - key: key, - value: value, - }, - }); - document.addEventListener(`setStorage-${key}-${date}`, (evt) => { - resolve(evt.detail.value); - }); - document.dispatchEvent(event); - } catch (err) { - console.error(err); - } - }); - }, - getItem(key) { - return new Promise((resolve, reject) => { - try { - const date = Date.now(); - const event = new CustomEvent("requestStorage", { - detail: { - method: `getStorage-${key}-${date}`, - key: key, - date: date, - }, - }); - document.addEventListener(`getStorage-${key}-${date}`, (evt) => { - resolve(evt.detail.value); - }); - document.dispatchEvent(event); - } catch (err) { - console.error(err); - } - }); - }, - }; + injectScript(link) { + const scriptTag = document.createElement("script"); + scriptTag.src = this.extensionURL(link); - function getExtensionUrl(link) { - if (EXTENSION_ID) { - const prefix = is.chrome() ? "chrome-extension://" : "moz-extension://"; - return prefix + EXTENSION_ID + "/" + link.replace(/^\//, ""); - } else { - return `https://next-extension.captain.sv.ufabcnext.com/static/${link}`; - } + (document.head || document.documentElement).appendChild(scriptTag); } - var getFile = async function (link) { - return (await Axios.get(getChromeUrl(link))).data; - }; - - return { - getChromeUrl, - injectScript, - injectDiv, - injectIframe, - injectStyle, - fetchChromeUrl, - getExtensionUrl, - getFile, - storage, - }; + injectIframe(link) { + const iframeTag = document.createElement("iframe"); + iframeTag.src = this.extensionURL(link); + iframeTag.setAttribute("style", "display: none;"); + (document.body || document.documentElement).appendChild(iframeTag); + } } -export default new ExtensionUtils(); +export const extensionUtils = new ExtensionUtils(); diff --git a/src/views/Matricula.vue b/src/views/Matricula.vue index acd964e..b6730e1 100644 --- a/src/views/Matricula.vue +++ b/src/views/Matricula.vue @@ -91,8 +91,9 @@