-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
114 lines (102 loc) · 3.57 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';
var PAGE_TYPE = "";
const base_constants =
{
"album":
{
"collectionName":"type",
"colorPath" : "coverArt.extractedColors.colorRaw.hex",
},
"track":
{
"collectionName":"__typename",
"colorPath" : "albumOfTrack.coverArt.extractedColors.colorRaw.hex",
},
"playlist":{
"collectionName": "__typename",
}
}
document.addEventListener('DOMContentLoaded', function (){
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
const currentTabUrl = tabs[0].url;
if (currentTabUrl.includes("spotify.com")) {
let keyFetch = Object.keys(base_constants).find(key => currentTabUrl.includes(key))
if(keyFetch){
if(keyFetch=="playlist"){
loadHtml("./playlist/playlist_main.html","body",function (){
loadJS("./playlist/playlist.js")
PAGE_TYPE = keyFetch;
});
}else{
loadHtml("./album_track/album_track_main.html","body",function (){
loadJS("./album_track/album_track.js")
PAGE_TYPE = keyFetch;
});
}
}
else{
loadHtml("message_main.html","body",function (){
const message_space = document.getElementById("message_space");
message_space.innerHTML = "Open a Track, Album, Playlist";
});
}
} else {
loadHtml("message_main.html","body",function (){
const message_space = document.getElementById("message_space");
message_space.innerHTML = "This site is not";
});
}
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
});
});
function changePages(page_name){
document.getElementById("body").innerHTML = "";
loadHtml(`./${page_name}/${page_name}_main.html`,"body",function (){
loadJS(`./${page_name}/${page_name}.js`)
});
}
function numberCommas(number) {
let strNumber = number.toString();
strNumber = strNumber.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return strNumber;
}
function capFirst(str) {
str = str.toLowerCase();
return str.charAt(0).toUpperCase() + str.slice(1);
}
function filterByPath(obj, path) {
const keys = path.split('.');
let value = obj;
for (const key of keys) {
if (value.hasOwnProperty(key)) {
value = value[key];
} else {
return null;
}
}
return value;
}
function loadHtml(filename, targetId, callback) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
document.getElementById(targetId).innerHTML = xhr.responseText;
if (typeof callback === 'function') {
callback();
}
} else {
console.error('Error loading HTML file:', xhr.statusText);
}
}
};
xhr.open('GET', filename, true);
xhr.send();
}
function loadJS(filename) {
const script = document.createElement('script');
script.src = filename;
document.body.appendChild(script);
}