Skip to content

Commit

Permalink
Merge pull request #13 from lmbringas/master
Browse files Browse the repository at this point in the history
Add save track feature.
  • Loading branch information
jerosa committed Mar 4, 2019
2 parents 9c47fb5 + 880b567 commit 68f822d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
86 changes: 47 additions & 39 deletions background.js
Expand Up @@ -21,49 +21,57 @@ const gettingStoredSettings = browser.storage.sync.get();
gettingStoredSettings.then(checkStoredSettings, error => console.error(error));


function runCommand(command) {
browser.tabs.query({ url: "https://*.spotify.com/*" }, (tabs) => {
// Open a spotify tab if one does not exist yet.
if (tabs.length === 0) {
const gettingItem = browser.storage.sync.get("openSpotify");
gettingItem.then((res) => {
// check if user has enabled the option
if (res.openSpotify) browser.tabs.create({ url: "https://open.spotify.com" });
}, e => console.error(e));
}
async function runCommand(command) {
const tabs = await browser.tabs.query({ url: "https://*.spotify.com/*" });
// Open a spotify tab if one does not exist yet.
if (tabs.length === 0) {
const gettingItem = browser.storage.sync.get("openSpotify");
gettingItem.then((res) => {
// check if user has enabled the option
if (res.openSpotify) browser.tabs.create({ url: "https://open.spotify.com" });
}, e => console.error(e));
}

// Apply command
for (const tab of tabs) {
let code = "";
if (tab.url.startsWith("https://play.spotify.com")) {
code = `document.getElementById('app-player').contentDocument.getElementById('${command}').click()`;
} else if (tab.url.startsWith("https://open.spotify.com")) {
switch (command) {
case "play-pause":
code = "(document.querySelector('.spoticon-play-16') || document.querySelector('.spoticon-pause-16')).click()";
break;
case "next":
code = "document.querySelector('.spoticon-skip-forward-16').click()";
break;
case "previous":
code = "document.querySelector('.spoticon-skip-back-16').click()";
break;
case "shuffle":
code = "document.querySelector('.spoticon-shuffle-16').click()";
break;
case "repeat":
code = "(document.querySelector('.spoticon-repeat-16') || document.querySelector('.spoticon-repeatonce-16')).click()";
break;
case "play-album":
code = "document.querySelector('.btn-green').click()";
break;
// Apply command
for (const tab of tabs) {
let code = "";
if (tab.url.startsWith("https://play.spotify.com")) {
code = `document.getElementById('app-player').contentDocument.getElementById('${command}').click()`;
} else if (tab.url.startsWith("https://open.spotify.com")) {
switch (command) {
case "play-pause":
code = "(document.querySelector('.spoticon-play-16') || document.querySelector('.spoticon-pause-16')).click()";
break;
case "next":
code = "document.querySelector('.spoticon-skip-forward-16').click()";
break;
case "previous":
code = "document.querySelector('.spoticon-skip-back-16').click()";
break;
case "shuffle":
code = "document.querySelector('.spoticon-shuffle-16').click()";
break;
case "repeat":
code = "(document.querySelector('.spoticon-repeat-16') || document.querySelector('.spoticon-repeatonce-16')).click()";
break;
case "play-album":
code = "document.querySelector('.btn-green').click()";
break;
case "track-add": {
// CHECK: Region difference (heart/add)
let checkCode = "document.querySelector('.control-button').classList.contains('spoticon-heart-16') || ";
checkCode += "document.querySelector('.control-button').classList.contains('spoticon-heart-active-16')";
const res = await browser.tabs.executeScript(tab.id, { code: checkCode });
if (res[0]) code = "(document.querySelector('.spoticon-heart-16') || document.querySelector('.spoticon-heart-active-16')).click()";
else code = "(document.querySelector('.spoticon-add-16') || document.querySelector('.spoticon-added-16')).click()";
break;
}
}
if (code.length) {
browser.tabs.executeScript(tab.id, { code: code });
}
}
});
if (code.length) {
browser.tabs.executeScript(tab.id, { code: code });
}
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions manifest.json
Expand Up @@ -3,7 +3,7 @@
"description": "Keyboard shortcuts to play, pause, next and previous tracks in Spotify Web Player.",
"manifest_version": 2,
"author": "TsunDoge",
"version": "1.3.2",
"version": "1.4.0",
"homepage_url": "https://github.com/TsunDoge/spotify-hotkeys-firefox",
"applications": {
"gecko": {
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"shuffle": {
"suggested_key": {
"default": "Alt+Shift+F"
"default": "Alt+Shift+D"
},
"description": "Shuffle playlist"
},
Expand All @@ -53,6 +53,12 @@
"default": "Alt+Shift+B"
},
"description": "Play/pause the current playlist/album"
},
"track-add": {
"suggested_key": {
"default": "Alt+Shift+F"
},
"description": "Save track"
}
},
"permissions": [ "https://open.spotify.com/*", "https://play.spotify.com/*", "tabs", "storage" ],
Expand Down

0 comments on commit 68f822d

Please sign in to comment.