Skip to content

Commit

Permalink
Cleanup, new version
Browse files Browse the repository at this point in the history
  • Loading branch information
SleekPanther committed Jul 11, 2018
1 parent 36d177e commit 89ebe2d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 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 youtubeBaseUrl = 'youtube.com' //lowercase matters
const youtubeDomainMatch = '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 },
pageUrl: { hostContains: youtubeDomainMatch },
})
],
// And shows the extension's page action
Expand All @@ -19,22 +19,22 @@ 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 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
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

//Browser action instead of popup
//Run code on page action instead of popup
chrome.pageAction.onClicked.addListener(tab=>{
appendToUrl()
})

function appendToUrl(){
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, tabs=>{
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, tabs=>{ //Get current tab
let currentTab = tabs[0]
let oldURL = currentTab.url
let regexMatch = oldURL.match(regexMatchVideoId)
let regexMatch = oldURL.match(regexToExtractVideoId)
if(!regexMatch){ //not a valid youtube video URL (prevents keyboard shotcut override)
return
}
let videoId = regexMatch[1] //get video id
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
})
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.0",
"version_name": "1.0.0",
"version": "1.0.1",
"version_name": "1.0.1",

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

0 comments on commit 89ebe2d

Please sign in to comment.