-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #7.
- Loading branch information
Showing
12 changed files
with
237 additions
and
197 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import storage from 'storage/storage'; | ||
import {findNode} from 'utils/common'; | ||
|
||
const contentStorage = { | ||
autoplay: false, | ||
videoNodes: [] | ||
}; | ||
|
||
function addVideo(video) { | ||
if (!contentStorage.videoNodes.includes(video)) { | ||
contentStorage.videoNodes.push(video); | ||
|
||
const events = ['play', 'progress', 'ended']; | ||
for (const event of events) { | ||
video.addEventListener(event, setAutoplay); | ||
} | ||
} | ||
} | ||
|
||
function removeVideo(video) { | ||
const index = contentStorage.videoNodes.indexOf(video); | ||
|
||
if (index !== -1) { | ||
contentStorage.videoNodes.splice(index, 1); | ||
|
||
const events = ['play', 'progress', 'ended']; | ||
for (const event of events) { | ||
video.removeEventListener(event, setAutoplay); | ||
} | ||
} | ||
} | ||
|
||
async function setAutoplay() { | ||
const button = await findNode( | ||
'.ytp-right-controls button[data-tooltip-target-id="ytp-autonav-toggle-button"]', | ||
{throwError: false} | ||
); | ||
|
||
if (button) { | ||
const isOn = button.querySelector('[aria-checked="true"]') !== null; | ||
if (contentStorage.autoplay !== isOn) { | ||
button.click(); | ||
} | ||
} | ||
} | ||
|
||
function syncAutoplay(autoplay) { | ||
contentStorage.autoplay = autoplay; | ||
|
||
setAutoplay(); | ||
} | ||
|
||
function syncPlaylistAutoplay(autoplay) { | ||
const script = document.createElement('script'); | ||
script.textContent = `(async function() { | ||
const manager = await ${findNode.toString()}('yt-playlist-manager', { | ||
throwError: false | ||
}); | ||
if (manager) { | ||
Object.defineProperty(manager, 'canAutoAdvance_', { | ||
get: function() { | ||
return ${autoplay}; | ||
}, | ||
set: function() {} | ||
}); | ||
} | ||
})()`; | ||
document.documentElement.appendChild(script); | ||
script.remove(); | ||
} | ||
|
||
async function syncState() { | ||
const {autoplay, autoplayPlaylist} = await storage.get([ | ||
'autoplay', | ||
'autoplayPlaylist' | ||
]); | ||
|
||
syncAutoplay(autoplay); | ||
syncPlaylistAutoplay(autoplayPlaylist); | ||
} | ||
|
||
function onMessage(request, sender) { | ||
// Samsung Internet 13: extension messages are sometimes also dispatched | ||
// to the sender frame. | ||
if (sender.url === document.URL) { | ||
return; | ||
} | ||
|
||
if (request.id === 'syncState') { | ||
syncState(); | ||
} | ||
} | ||
|
||
function setup() { | ||
const videos = document.getElementsByTagName('video'); | ||
for (const video of videos) { | ||
addVideo(video); | ||
} | ||
|
||
const observer = new MutationObserver(function (mutations) { | ||
mutations.forEach(function (mutation) { | ||
mutation.addedNodes.forEach(function (node) { | ||
if (node.nodeName.toLowerCase() === 'video') { | ||
addVideo(node); | ||
} | ||
}); | ||
|
||
mutation.removedNodes.forEach(function (node) { | ||
if (node.nodeName.toLowerCase() === 'video') { | ||
removeVideo(node); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
observer.observe(document, {childList: true, subtree: true}); | ||
|
||
syncState(); | ||
} | ||
|
||
function init() { | ||
browser.runtime.onMessage.addListener(onMessage); | ||
|
||
setup(); | ||
} | ||
|
||
init(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.