Skip to content

Instantly share code, notes, and snippets.

@Phate334
Last active June 10, 2019 16:29
Show Gist options
  • Save Phate334/860751ddee158419220a10238330b53c to your computer and use it in GitHub Desktop.
Save Phate334/860751ddee158419220a10238330b53c to your computer and use it in GitHub Desktop.
在 Facebook Creator Studio 音樂庫中自動切換下一首歌曲。https://www.facebook.com/creatorstudio/
// Auto switch to next one when end of a song.
// start playing a song then paste this code.
(function() {
function getElementByXpath(path) {
return document.evaluate(
path,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
var timecodeNow =
"/html/body/div[1]/div/div[2]/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[4]/div/div[2]/div/div[2]/div[1]";
var timecodeEnd =
"/html/body/div[1]/div/div[2]/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[4]/div/div[2]/div/div[2]/div[3]";
var nextSong =
"/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/div[2]/div[2]/div/div[4]/div/div[2]/div/div[1]/a[5]";
var eleNow = getElementByXpath(timecodeNow);
var eleEnd = getElementByXpath(timecodeEnd);
var eleNext = getElementByXpath(nextSong);
// https://codepen.io/webgeeker/pen/YjrZgg
const MutationObserver =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
const config = {
attributes: true,
childList: true,
characterData: true,
subtree: true
};
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === "characterData") {
if (
eleNow.innerText === eleEnd.innerText &&
eleEnd.innerText !== "0:00"
) {
eleNext.click();
}
}
});
});
observer.observe(eleNow, config);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment