|
1 | 1 | import { registerUrl } from "@/utils";
|
2 | 2 | import { main as vocabMountain } from "./vocab-mountain";
|
| 3 | +import { greVocabMountain, toeflVocabMountain } from "@/utils/storage"; |
3 | 4 |
|
4 | 5 | export default defineContentScript({
|
5 | 6 | matches: ["*://*.gregmat.com/*"],
|
6 | 7 | runAt: "document_idle",
|
7 | 8 | main() {
|
8 | 9 | registerUrl("vocab-mountain", vocabMountain);
|
| 10 | + storeVocab(); |
9 | 11 | },
|
10 | 12 | });
|
| 13 | + |
| 14 | +async function storeVocab() { |
| 15 | + const greReq = fetch("https://www.gregmat.com/mountains/vocab-mountain"); |
| 16 | + const toeflReq = fetch( |
| 17 | + "https://www.gregmat.com/mountains/toefl-vocab-mountain" |
| 18 | + ); |
| 19 | + |
| 20 | + const req = await Promise.all([greReq, toeflReq]); |
| 21 | + |
| 22 | + const body = await Promise.all([req[0].text(), req[1].text()]); |
| 23 | + |
| 24 | + const parser = new DOMParser(); |
| 25 | + |
| 26 | + const lookup = [greVocabMountain, toeflVocabMountain] as const; |
| 27 | + |
| 28 | + body.forEach((b, idx) => { |
| 29 | + const doc = parser.parseFromString(b, "text/html"); |
| 30 | + |
| 31 | + const data = doc.getElementById("__NUXT_DATA__"); |
| 32 | + if (!data) return; |
| 33 | + |
| 34 | + const json = JSON.parse(data.innerHTML); |
| 35 | + |
| 36 | + const processed = json[7].map((idx: number) => |
| 37 | + json[json[idx].mountain_contents] |
| 38 | + .map((entry: number) => json[entry]) |
| 39 | + .map((word: any) => { |
| 40 | + const temp = document.createElement("div"); |
| 41 | + temp.innerHTML = json[word.description]; |
| 42 | + return { |
| 43 | + title: json[word.title], |
| 44 | + description: json[word.description], |
| 45 | + pronunciation: json[word.pronunciation], |
| 46 | + text: temp.textContent?.replaceAll(/[\n\t]+/g, " ").trim() ?? "", |
| 47 | + }; |
| 48 | + }) |
| 49 | + ); |
| 50 | + |
| 51 | + lookup[idx].setValue(processed); |
| 52 | + }); |
| 53 | +} |
0 commit comments