-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
50 lines (45 loc) · 1.9 KB
/
background.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
//Only make extension icon visible on YouTube.com
const youtubeVideoWatchPageUrl = 'youtube.com/watch?v=' //lowercase matters
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlContains: youtubeVideoWatchPageUrl },
})
],
// And shows the extension's page action
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}])
})
})
const urlPrerfix = 'https://www.'
const YOUTUBE_VIDEO_URL_START = urlPrerfix + youtubeVideoWatchPageUrl //a youtube url WITHOUT the videoID
const regexToExtractVideoId = /youtube.com\/watch\?v=([^&\?]*)/i //case insensitive regular expression matches youtube.com/watch?v=[VIDEO_ID] & captures the video ID since the ID is either the end of the string or ends at a question mark or ampersand
const MAGIC_URL_POSTFIX = '&list=ULcxqQ59vzyTk' //thing to append to make it play chronologically
//Run code on page action instead of popup
chrome.action.onClicked.addListener(tab=>{
appendToUrl()
});
function appendToUrl(){
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, tabs=>{ //Get current tab
let currentTab = tabs[0]
let oldURL = currentTab.url
let regexMatch = oldURL.match(regexToExtractVideoId)
if(!regexMatch){ //not a valid youtube video URL (prevents keyboard shortcut override)
return
}
let videoId = regexMatch[1] //get video id from regex match
let newUrl = YOUTUBE_VIDEO_URL_START + videoId + MAGIC_URL_POSTFIX
chrome.tabs.update(currentTab.id, {url: newUrl}) //update tab/reloads page with new location
})
}
//Keyboard Shortcut
chrome.commands.onCommand.addListener(command => {
if(command === 'appendToUrl'){
appendToUrl()
}
})
self.oninstall = () => self.skipWaiting();