-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.js
114 lines (103 loc) · 3.63 KB
/
requests.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
var idStack = [];
var skipStack = [];
var hiddenSongs = [];
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var result = details.url.match(/tracks\/\d+\/streams/);
var id = result[0].match(/\d+/)[0]
idStack.push(id);
},
{urls: ["*://api.soundcloud.com/i1/*/streams*"]}
);
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var result = details.url.match(/tracks\/\d+\/plays/);
var id = result[0].match(/\d+/)[0]
var isHidden;
chrome.storage.sync.get(id, function (hiddenSong) {
console.info(hiddenSong);
isHidden = hiddenSong != null;
});
if (isHidden) {
notifySkip();
}
return { cancel: isHidden } //Stops from sending request a skip.
},
{ urls: ["*://api.soundcloud.com/tracks/*/plays*"] },
["blocking"]
);
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var idMatchresult = details.url.match(/A\d+&source/);
var id = idMatchresult[0].match(/\d+/)[0]
var isHidden = hiddenSongs.indexOf(id) != -1;
if (!isHidden) {
//Song isn't hidden, proceed as normal
console.log("Song with ID : " + id + " is not hidden, proceed.")
clearSkipStack();
return;
}
var triggerMatch = details.url.match(/&trigger=auto&/);
var trigger = triggerMatch == null ? "manual" : "auto";
var skipDirection;
if (trigger == "manual" && skipStack.length > 0) {
//user pressed skip, check if song is hidden
skipDirection = skipStack.pop();
}
clearSkipStack();
if (!skipDirection) {
skipDirection = "forward";
}
//signal schide.js to skip forward
console.log("Skipping song from background ID: " +id + " Direction : " +skipDirection)
chrome.tabs.query({ url: "https://soundcloud.com/*" }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { request: "skipSong", skipDirection: skipDirection }, function () {
});
});
},
{ urls: ["*://eventlogger.soundcloud.com/audio?anonymous_id=*action=play*"] }
);
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
clearSkipStack();
var result = details.url.match(/skip.*context/);
var skipMatch = result[0].match(/back/);
var skip = skipMatch == null ? "forward" : "back"
console.log("Received skip : " + skip);
skipStack.push(skip)
},
{ urls: ["*://eventlogger.soundcloud.com/click?*=*3Askip_*"] }
);
function clearSkipStack()
{
while(skipStack.length > 0) {
skipStack.pop();
}
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.request == "lastId") {
if (idStack.length == 0) {
sendResponse({ id: "" });
}
var lastId = idStack[idStack.length - 1];
sendResponse({ id: lastId });
return;
}
else if (request.request == "hideButton") {
if (request.id == "" || request.id == null) {
return
}
hiddenSongs.push(request.id);
var song = {};
song[request.id] = request.songName;
chrome.storage.sync.set(song, function () {
});
sendResponse({ "message": 'Song ID:' + request.id + " - " + request.songName + " saved as hidden." });
}
else if (request.request == "isHidden") {
if (request.id == "" || request.id == null) {
sendResponse({isHidden : "empty"})
}
var response = isHidden(request.id) ? "true" : "false";
}
});