diff --git a/background.js b/background.js new file mode 100644 index 0000000..fc1a9f9 --- /dev/null +++ b/background.js @@ -0,0 +1,49 @@ +let jobs = []; +let start = false; + +function addPageToURL(url) { + const regex = /page=(\d+)/; + const match = url.match(regex); + const page = (match && match[1]) || "1"; + const newPage = parseInt(page) + 1; + + return url.replace(regex, `page=${newPage}`); +} + +async function changeTabtoNextPage(url, tabid) { + const newURL = addPageToURL(url); + await chrome.tabs.update(tabid, { url: newURL }); +} + +chrome.runtime.onConnect.addListener(function (port) { + port.onMessage.addListener(async function (params) { + const { cmd } = params; + if (cmd === "start") { + start = true; + const [tab] = await chrome.tabs.query({ + active: true, + currentWindow: true, + }); + + if (tab) { + let port = chrome.tabs.connect(tab.id, { name: "bg-content_script" }); + port.postMessage({ cmd: "scrap" }); + } + } + + if (cmd === "getInfo") { + const { jobsInformation} = params; + jobs = [...jobsInformation]; + + const jobsJSON = jobs; + + chrome.storage.local.set({ jobs }, () => { + console.log("Data saved to local storage", jobs); + }); + + start = false; + + chrome.runtime.sendMessage({ cmd: "dataJobs", jobs: jobsJSON }); + } + }); +}); diff --git a/contentscript.js b/contentscript.js new file mode 100644 index 0000000..206f015 --- /dev/null +++ b/contentscript.js @@ -0,0 +1,50 @@ +function getJobInformation() { + let jobElementInformation = document.querySelectorAll("div[id*=jobcard]"); + jobElementInformation = [...jobElementInformation]; + + const jobJsonInformation = jobElementInformation.map((el) => { + const [ + {}, + { + children: [ + { + children: [ + { innerText: dateJob }, + {}, + { innerText: jobSalaryRange }, + ], + }, + ], + }, + ] = el.children; + salaryRange = jobSalaryRange.split("\n")[0]; + date = dateJob.split("\n")[0]; + country = el.querySelector("p[class*=zonesLinks]").innerText; + return { date, salaryRange, country }; + }); + + const filterDate = jobJsonInformation.filter((e) => e.date === "Hoy"); + + const filterCountry = filterDate.filter((value, index, array) => { + + return array.findIndex(obj => obj.country === value.country && obj.salaryRange === value.salaryRange) === index; + }); + + return filterCountry; +} + +const portBackground = chrome.runtime.connect({ + name: "content_script-background", +}); + +portBackground.postMessage({ cmd: "online" }); + +chrome.runtime.onConnect.addListener(function (port) { + port.onMessage.addListener(({ cmd }) => { + if (cmd === "scrap") { + const jobsInformation = getJobInformation(); + + portBackground.postMessage({ cmd: "getInfo", jobsInformation }); + } + }); +}); diff --git a/images/icon.png b/images/icon.png new file mode 100644 index 0000000..25057ee Binary files /dev/null and b/images/icon.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..33ff2c8 --- /dev/null +++ b/manifest.json @@ -0,0 +1,22 @@ +{ + "manifest_version": 3, + "name": "Krowdy-reto-1", + "version": "1.0", + "description": "", + "action": { + "default_popup": "./popup/index.html" + }, + "permissions": ["storage", "activeTab", "scripting", "tabs"], + "icons": { + "32": "./images/icon.png" + }, + "background": { + "service_worker": "./background.js" + }, + "content_scripts": [ + { + "matches": ["https://www.occ.com.mx/*"], + "js": ["./contentscript.js"] + } + ] +} diff --git a/popup/index.html b/popup/index.html new file mode 100644 index 0000000..721e577 --- /dev/null +++ b/popup/index.html @@ -0,0 +1,52 @@ + + + + + + + + Krowdy-reto-1 + + + + + + +
+ + + + \ No newline at end of file diff --git a/popup/index.js b/popup/index.js new file mode 100644 index 0000000..3f07c0c --- /dev/null +++ b/popup/index.js @@ -0,0 +1,36 @@ +const btnScripting = document.getElementById("btnScript"); +const btnClearStorage = document.getElementById("btnClearStorage"); + +const jobCard = document.getElementById("jobCard"); + + +btnScripting.addEventListener("click", async () => { + chrome.storage.local.remove(["jobs"]); + jobCard.textContent = "Procesando..."; + setTimeout(() => { + jobCard.textContent = ""; + var port = chrome.runtime.connect({ name: "popup-background" }); + port.postMessage({ cmd: "start" }); + }, 3000); + +}); + + +chrome.runtime.onMessage.addListener(function (message) { + const { cmd, jobs } = message; + let html = ""; + if (cmd === "dataJobs" && jobs) { + jobs.forEach((e) => { + html += ` +
+

${e.country}

+

${e.salaryRange}

+
+ `; + }); + jobCard.innerHTML = html; + + } +}); + +