Skip to content

Commit

Permalink
Converted to pageAction so only appears on YouTube.com
Browse files Browse the repository at this point in the history
  • Loading branch information
SleekPanther committed Jul 10, 2018
1 parent 9b0b3a5 commit 615691d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
26 changes: 22 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
//Only make extension icon visible on YouTube.com
const youtubeBaseUrl = 'youtube.com' //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: youtubeBaseUrl },
})
],
// And shows the extension's page action
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}])
})
})


const MAGIC_URL_POSTFIX = '&list=ULcxqQ59vzyTk'
const YOUTUBE_VIDEO_URL_START = 'https://www.youtube.com/watch?v='
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 regexMatchVideoId = /youtube.com\/watch\?v=([^&\?]*)/ //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

//Browser action instead of popup
chrome.browserAction.onClicked.addListener(tab=>{
chrome.pageAction.onClicked.addListener(tab=>{
appendToUrl()
})

Expand All @@ -14,7 +32,7 @@ function appendToUrl(){
let oldURL = currentTab.url
let videoId = oldURL.match(regexMatchVideoId)[1] //get video id
let newUrl = YOUTUBE_VIDEO_URL_START + videoId + MAGIC_URL_POSTFIX
chrome.tabs.update(currentTab.id, {url: newUrl})
chrome.tabs.update(currentTab.id, {url: newUrl}) //update tab/reloads page with new location
})
}

Expand Down
10 changes: 6 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

"name": "YouTube Chronological Order",
"description": "Watch YouTube videos in chronological order from any channel",
"version": "0.3.0",
"version_name": "0.3.0",
"version": "0.4.0",
"version_name": "0.4.0",

"browser_action": {
"page_action": {
// "default_icon": "assets/icons/icon.png",
"default_title": "(Alt+P) Watch channel videos in chronological order"
},
"permissions": [
"declarativeContent",
"activeTab",
"tabs"
],
"background": {
"scripts": [
"background.js"
]
],
"persistent": false
},
"commands": {
"appendToUrl": {
Expand Down

0 comments on commit 615691d

Please sign in to comment.