-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
69 lines (62 loc) · 2.53 KB
/
contentScript.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
const songsOnPage = 16;
const detailedPageRegex = /.*whosampled.com\/\S*\/\S+\/\S+\/$/;
function OpenLinks(songURLs, songName, artistName) {
chrome.runtime.sendMessage({links: songURLs, song: songName, artist: artistName}, function(response) {
console.log(response.status);
if (response.status != "OK") {
console.log(response.message);
}
});
}
function JustOpenVisible(section, songName, artistName) {
let songsToOpen = section.querySelectorAll('div.list > div.listEntry > a');
songsToOpen = [...songsToOpen].map(link => link.href);
return songsToOpen;
}
async function FetchAndOpenAll (url) {
let songURLs = [];
// Naive implementation, fetch one page after another
await fetch(url).then(response => response.text())
.then(async text => {
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(text, "text/html");
songURLs = [...songURLs, ...[...htmlDocument.querySelectorAll("div.list > div.listEntry > a")].map(link => link.href)];
const nextBtn = htmlDocument.querySelector('div.pagination > span.next > a');
if (nextBtn) {
let songsFromNextPage = await FetchAndOpenAll(nextBtn.href);
songURLs = [...songURLs, ...songsFromNextPage];
}
});
return songURLs;
}
async function OpenAll(btn) {
const section = btn.parentElement.parentElement;
const sectionTitle = section.querySelector('header > span.section-header-title').innerText;
const seeAllBtn = section.querySelector('header > a.moreButton');
const songName = document.querySelector('div.trackInfo > h1 > meta[itemprop="name"]').content;
const artistName = document.querySelector('div.trackInfo > h1 > span.trackArtistNames > meta[itemprop="name"]').content;
let songURLs;
if (!seeAllBtn) {
if (detailedPageRegex.test(window.location.href)) {
songURLs = await FetchAndOpenAll(window.location.href, songName, artistName, sectionTitle);
} else {
songURLs = JustOpenVisible(section, songName, artistName);
}
} else {
songURLs = await FetchAndOpenAll(seeAllBtn.href, songName, artistName, sectionTitle);
}
OpenLinks(songURLs, songName, artistName);
}
function CreateOpenAllButton () {
let openAllButton = document.createElement('button');
openAllButton.textContent = "open all";
openAllButton.style.marginLeft = "5px";
openAllButton.classList.add('moreButton');
openAllButton.onclick = () => OpenAll(openAllButton);
return openAllButton;
}
console.log("Loaded..");
let sectionHeaders = document.querySelectorAll('section > header.sectionHeader');
sectionHeaders.forEach(header => {
header.insertBefore(CreateOpenAllButton(), null);
});