-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
41 lines (37 loc) · 1.08 KB
/
popup.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
const render = (content) => {
document.getElementById('app').innerHTML = content;
};
const renderNotices = (notices) =>
render(
notices
.map(({ message, contributor: { name } }) => {
return `<article>
${message}
<em>${name}</em>
</article>`;
})
.join('<hr />')
);
const createRuleMatcher = (url) => ({ conditions }) =>
conditions
.map((c) => c?.pageUrl?.urlMatches)
.filter((urlMatches) => urlMatches)
.some((urlMatches) => url.match(urlMatches));
try {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const ruleMatcher = createRuleMatcher(tabs[0].url);
chrome.declarativeContent.onPageChanged.getRules(async (rules) => {
const noticesIds = rules.filter(ruleMatcher).map(({ id }) => id);
const notices = await Promise.all(
noticesIds.map((noticeId) =>
fetch(
`https://notices.bulles.fr/api/v3/notices/${noticeId}`
).then((response) => response.json())
)
);
renderNotices(notices);
});
});
} catch (e) {
console.error(e);
}