Created
March 23, 2019 05:58
-
-
Save championshuttler/dc6ddb8d3d87ef64588faa014896a26b to your computer and use it in GitHub Desktop.
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
var commandToExecute = ''; | |
browser.commands.onCommand.addListener(on_command_received); | |
function on_command_received(command_name) { | |
if (command_name == 'next-song') { | |
commandToExecute = 'document.querySelector(".ytp-next-button.ytp-button").click()'; | |
} else if (command_name == 'pp-song') { | |
commandToExecute = 'document.querySelector(".ytp-play-button.ytp-button").click()'; | |
} | |
// Query tabs based on url | |
var querying = browser.tabs.query({ | |
url: "*://*.youtube.com/*" | |
}); | |
querying.then(executeOnTab, onError); | |
} | |
function executeOnTab(tabs) { | |
var tabID = tabs[0].id; // getting the first tab ID alone | |
var executing = browser.tabs.executeScript( | |
tabID, { | |
code: commandToExecute | |
} | |
); | |
executing.then(onExecuted, onError); | |
} | |
function onError(error) { | |
console.log(`Error: ${error}`); | |
} | |
function onExecuted(result) { | |
console.log(`We executed script in the first youtube tab`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment