-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
79 lines (64 loc) · 2.19 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{
const CARDMARKET = "Cardmarket";
const TCGPLAYER = "TCGPlayer";
const sendDeckListMessage = (deckList) => {
if (!deckList) return;
chrome.runtime.sendMessage({
type: "deck-list-message",
data: deckList,
});
};
// https://www.cardmarket.com/en/Vanguard/Expansions
const parseCardFormat = (deckFormat, cardFormat) => {
if (deckFormat === "Standard") return "D";
if (deckFormat === "V Premium") return "V";
const legacySets = {
G: ["G-", "D-PS", "V-SS01", "V-SS07", "V-SS09"],
P: ["D-PS"],
};
const format = Object.keys(legacySets).find((setsArray) =>
legacySets[setsArray].find((set) => cardFormat.includes(set))
);
return format;
};
const getDeckListFromDom = (store = CARDMARKET, showSet = false) => {
const deckFormat = document
.querySelector(".deck-preview-top-info")
?.firstChild.innerHTML.split(":")[1]
.trim();
const cardData = document.querySelectorAll(".card-controller-inner");
let deckList = "";
cardData.forEach((data) => {
const cardTitle = data.firstChild.title;
const cardFormat = cardTitle.split("/")[0];
const parsedFormat = parseCardFormat(deckFormat, cardFormat);
let name = cardTitle.split(/:(.*)/s)[1].trim();
const cleanName = name;
const number = data.lastChild.innerText;
if (store === CARDMARKET) {
name = `${name.concat(` [${parsedFormat} Format]`)}`;
} else if (store === TCGPLAYER) {
name = name.replaceAll("'", '"');
if (showSet) {
name = name.concat(` [${cardFormat}]`);
}
}
deckList = deckList.concat(
`<span class="card-name" data-title="${cleanName}">${number} ${name}</span>`
);
});
sendDeckListMessage(deckList);
};
chrome.runtime.onMessage.addListener((message) => {
const { type, data } = message;
if (type === "store-change") {
getDeckListFromDom(data);
} else if (type === "show-set") {
getDeckListFromDom(data.store, data.checked);
}
});
// On launch, get the user's saved store and generate the card list
chrome.storage.sync.get("store", function ({ store }) {
getDeckListFromDom(store);
});
}