2022-04-01 01:40:39 +00:00
|
|
|
/*
|
|
|
|
content script running on youtube.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
let browserType = getBrowser();
|
|
|
|
|
|
|
|
|
|
|
|
// boilerplate to dedect browser type api
|
|
|
|
function getBrowser() {
|
|
|
|
if (typeof chrome !== "undefined") {
|
|
|
|
if (typeof browser !== "undefined") {
|
|
|
|
console.log("detected firefox");
|
|
|
|
return browser;
|
|
|
|
} else {
|
|
|
|
console.log("detected chrome");
|
|
|
|
return chrome;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("failed to dedect browser");
|
|
|
|
throw "browser detection error"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-31 08:45:25 +00:00
|
|
|
function detectUrlType(url) {
|
|
|
|
|
2022-06-17 09:47:40 +00:00
|
|
|
const videoRe = new RegExp(/^https:\/\/(www\.)?(youtube.com\/watch\?v=|youtu\.be\/)[\w-]{11}/);
|
2022-05-31 08:45:25 +00:00
|
|
|
if (videoRe.test(url)) {
|
|
|
|
return "video"
|
|
|
|
}
|
2022-06-17 09:47:40 +00:00
|
|
|
const channelRe = new RegExp(/^https:?\/\/www\.?youtube.com\/c|channel|user\/[\w-]+(\/|featured|videos)?$/);
|
2022-05-31 08:45:25 +00:00
|
|
|
if (channelRe.test(url)) {
|
|
|
|
return "channel"
|
|
|
|
}
|
|
|
|
const playlistRe = new RegExp(/^https:\/\/(www\.)?youtube.com\/playlist\?list=/);
|
|
|
|
if (playlistRe.test(url)) {
|
|
|
|
return "playlist"
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
function sendUrl() {
|
|
|
|
|
2022-05-31 08:45:25 +00:00
|
|
|
let url = document.URL
|
|
|
|
|
|
|
|
let urlType = detectUrlType(url);
|
|
|
|
if (urlType == false) {
|
|
|
|
console.log("not relevant")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-01 01:40:39 +00:00
|
|
|
let payload = {
|
|
|
|
"youtube": {
|
2022-05-31 08:45:25 +00:00
|
|
|
"url": url,
|
|
|
|
"title": document.title,
|
|
|
|
"type": urlType,
|
2022-04-01 01:40:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 13:54:29 +00:00
|
|
|
console.log("youtube link: " + JSON.stringify(payload));
|
2022-04-01 01:40:39 +00:00
|
|
|
browserType.runtime.sendMessage(payload, function(response) {
|
|
|
|
console.log(response.farewell);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2022-04-01 07:44:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener("yt-navigate-finish", function (event) {
|
|
|
|
setTimeout(function(){
|
|
|
|
sendUrl();
|
|
|
|
return false;
|
|
|
|
}, 2000);
|
|
|
|
});
|