Skip to content

Commit

Permalink
Only activate extension on video WATCH pages
Browse files Browse the repository at this point in the history
  • Loading branch information
SleekPanther committed Jul 14, 2018
1 parent c4d61c3 commit ab1cf71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//Only make extension icon visible on YouTube.com
const youtubeDomainMatch = 'youtube.com' //lowercase matters
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: { hostContains: youtubeDomainMatch },
pageUrl: { urlContains: youtubeVideoWatchPageUrl },
})
],
// And shows the extension's page action
Expand All @@ -17,9 +17,10 @@ chrome.runtime.onInstalled.addListener(function() {
})


const MAGIC_URL_POSTFIX = '&list=ULcxqQ59vzyTk' //thing to append to make it play chronlogically
const YOUTUBE_VIDEO_URL_START = 'https://www.youtube.com/watch?v=' //a youtube url without the videoID
const regexToExtractVideoId = /youtube.com\/watch\?v=([^&\?]*)/i //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 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.pageAction.onClicked.addListener(tab=>{
Expand All @@ -31,7 +32,7 @@ function appendToUrl(){
let currentTab = tabs[0]
let oldURL = currentTab.url
let regexMatch = oldURL.match(regexToExtractVideoId)
if(!regexMatch){ //not a valid youtube video URL (prevents keyboard shotcut override)
if(!regexMatch){ //not a valid youtube video URL (prevents keyboard shortcut override)
return
}
let videoId = regexMatch[1] //get video id from regex match
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

"name": "YouTube Chronological Order",
"description": "Watch all videos from a channel in chronological order",
"version": "1.0.1",
"version_name": "1.0.1",
"version": "1.0.2",
"version_name": "1.0.2",

"page_action": {
"default_icon" : "assets/icons/chronological-icon-16.png",
Expand Down

0 comments on commit ab1cf71

Please sign in to comment.