Skip to content

Commit

Permalink
Merge pull request #22 from illuzor/manifest_v3
Browse files Browse the repository at this point in the history
Manifest v3
  • Loading branch information
illuzor authored Feb 24, 2023
2 parents c030ff1 + b68408a commit ded65f4
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 111 deletions.
64 changes: 39 additions & 25 deletions source/action-play.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,53 @@ for (i = 0; i < playButtonsBottom.length; i++) {
playButtons.push(playButtonsBottom[i])
}

if (playButtons.length != 0) {
if (playButtons) {
playButtons[playButtons.length - 1].click();
nothingToPlay = 0;
} else {
if (play != "none") {
chrome.storage.sync.get({play: "first"}, function (items) {
playNewPodcast(items.play)
});
}

var allPodcasts = document.querySelectorAll('.ReactVirtualized__Table__row.row.clickable');
var podcastsToPlay = new Array();
function playNewPodcast(play) {
if (play == "none") {
return
}

for (i = 0; i < allPodcasts.length; i++) {
if (!allPodcasts[i].classList.contains("played"))
podcastsToPlay[podcastsToPlay.length] = allPodcasts[i];
}
var allPodcasts = document.querySelectorAll('.ReactVirtualized__Table__row.row.clickable');
var podcastsToPlay = new Array();

if (podcastsToPlay.length) {
var positionToPlay;

switch (play) {
case "first":
positionToPlay = 0;
break;
case "last":
positionToPlay = podcastsToPlay.length - 1;
break;
case "random":
positionToPlay = Math.floor(Math.random() * podcastsToPlay.length);
break;
}
for (i = 0; i < allPodcasts.length; i++) {
if (!allPodcasts[i].classList.contains("played"))
podcastsToPlay[podcastsToPlay.length] = allPodcasts[i];
}

podcastsToPlay[positionToPlay].querySelector('[aria-label="Play"]').click();
nothingToPlay = 0;
if (podcastsToPlay.length) {
var positionToPlay;

switch (play) {
case "first":
positionToPlay = 0;
break;
case "last":
positionToPlay = podcastsToPlay.length - 1;
break;
case "random":
positionToPlay = Math.floor(Math.random() * podcastsToPlay.length);
break;
}

podcastsToPlay[positionToPlay].querySelector('[aria-label="Play"]').click();
nothingToPlay = 0;
}
}

nothingToPlay;
if (nothingToPlay == 1) {
chrome.storage.sync.get({ntp_enabled: true},
function (items) {
if (items.ntp_enabled) {
alert(chrome.i18n.getMessage('ntp'));
}
});
}
9 changes: 6 additions & 3 deletions source/action-skip.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var buttons = document.getElementsByClassName(type);
if (buttons.length)
buttons[0].click();
function performSkip(type) {
let buttons = document.getElementsByClassName(type);
if (buttons.length) {
buttons[0].click();
}
}
85 changes: 44 additions & 41 deletions source/background.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const URL = "https://play.pocketcasts.com/";

const ACTION_INIT = "init";
const ACTION_PLAY = "play";
const ACTION_FORWARD = "forward";
const ACTION_BACK = "back";

var pcTab;
var playFromMediaKey;
var action = ACTION_INIT;
var action = null;
var isFirstPlay = true

chrome.browserAction.onClicked.addListener(buttonClick);
chrome.action.onClicked.addListener(buttonClick);
chrome.commands.onCommand.addListener(mediaButtonPress);

chrome.runtime.onMessage.addListener(function (message, sender, resp) {
chrome.browserAction.setIcon({ path: "images/" + message.state + ".png" });
chrome.browserAction.setTitle({ title: chrome.i18n.getMessage(message.state) });
chrome.runtime.onMessageExternal.addListener(function (message, sender, resp) {
chrome.action.setIcon({path: "images/" + message.state + ".png"});
chrome.action.setTitle({title: chrome.i18n.getMessage(message.state)});
});

gotoGetWindows();
Expand All @@ -40,7 +40,7 @@ function mediaButtonPress(command) {
}

function playButtonPress() {
chrome.storage.sync.get({ play_enabled: true },
chrome.storage.sync.get({play_enabled: true},
function (items) {
if (items.play_enabled) {
action = ACTION_PLAY;
Expand All @@ -51,7 +51,7 @@ function playButtonPress() {
}

function skipButtonPress(skipDirection) {
chrome.storage.sync.get({ skip_enabled: true },
chrome.storage.sync.get({skip_enabled: true},
function (items) {
if (items.skip_enabled) {
action = skipDirection;
Expand All @@ -62,7 +62,7 @@ function skipButtonPress(skipDirection) {

function gotoGetWindows() {
pcTab = null;
chrome.windows.getAll({ populate: true }, getWindows);
chrome.windows.getAll({populate: true}, getWindows);
}

function getWindows(windows) {
Expand All @@ -78,23 +78,22 @@ function getWindows(windows) {
pcTab = pcTabs[0];
performAction();
} else {
if (action == ACTION_PLAY && !playFromMediaKey)
if (action == ACTION_PLAY && !playFromMediaKey) {
openNewTab();
}
}
}

function performAction() {
switch (action) {
case ACTION_INIT:
chrome.tabs.executeScript(pcTab.id, { file: "log-listener.js" });
break;
case ACTION_PLAY:
chrome.storage.sync.get({ play: "first" }, function (items) {
chrome.tabs.executeScript(pcTab.id, { code: 'var play = "' + items.play + '";' }, function () {
chrome.tabs.executeScript(pcTab.id, { file: "action-play.js" }, playPause);
chrome.tabs.executeScript(pcTab.id, { file: "log-listener.js" });
});
});
registerLogListener(pcTab.id)
if (isFirstPlay) {
isFirstPlay = false
setTimeout(performPlay, 100);
} else {
performPlay()
}
break;
case ACTION_FORWARD:
skip("skip-forward-button");
Expand All @@ -105,39 +104,43 @@ function performAction() {
}
}

function performPlay() {
chrome.scripting.executeScript({
target: {tabId: pcTab.id},
files: ["action-play.js"]
})
}

function openNewTab() {
chrome.browserAction.setIcon({ path: "images/Play.png" });
chrome.browserAction.setTitle({ title: chrome.i18n.getMessage("Play") });
chrome.storage.sync.get({ page: "default" }, function (items) {
chrome.action.setIcon({path: "images/Play.png"});
chrome.action.setTitle({title: chrome.i18n.getMessage("Play")});
chrome.storage.sync.get({page: "default"}, function (items) {
if (items.page != "none") {
var finalUrl = URL;
if (items.page != "default")
finalUrl += items.page;
chrome.storage.sync.get({ pin_tab: false }, function (items) {
chrome.tabs.create({ url: finalUrl, pinned: items.pin_tab });
chrome.tabs.query({
active: true, currentWindow: true
}, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, { file: "log-listener.js" });
chrome.storage.sync.get({pin_tab: false}, function (items) {
chrome.tabs.create({url: finalUrl, pinned: items.pin_tab}, function (tab) {
registerLogListener(tab.id)
});
});
}
});
}

function skip(type) {
chrome.tabs.executeScript(pcTab.id, { code: 'var type = "' + type + '";' },
function () {
chrome.tabs.executeScript(pcTab.id, { file: "action-skip.js" });
});
function registerLogListener(tabId) {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: ["log-listener-injector.js"]
})
}

function playPause(nothigToPlay) {
if (nothigToPlay == 1) {
chrome.storage.sync.get({ ntp_enabled: true },
function (items) {
if (items.ntp_enabled)
alert(chrome.i18n.getMessage('ntp'));
});
}
function skip(type) {
chrome.scripting.executeScript({target: {tabId: pcTab.id}, files: ["action-skip.js"]}, () => {
chrome.scripting.executeScript({
target: {tabId: pcTab.id},
args: [type],
func: (...args) => performSkip(...args),
});
});
}
7 changes: 7 additions & 0 deletions source/log-listener-injector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function injectLogListener(src) {
var script = document.createElement('script');
script.src = src;
(document.body || document.head || document.documentElement).appendChild(script);
}

injectLogListener(chrome.runtime.getURL('/log-listener.js'));
24 changes: 16 additions & 8 deletions source/log-listener.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
if (!script) {
if (typeof listenLog === 'undefined') {

const EXT_ID = "ogdnlmiknnmedpcnjnkjncdjjgfdkiik"
const EXT_ID_LOCAL = "gimcijegdcaeebbegnkglpgmpgmkeklo"

let extensionIds = [EXT_ID, EXT_ID_LOCAL]

window.addEventListener("LogEvent", function (e) {
var event = e.detail;
if (event.includes("[Audio] playing") || event.includes("[Audio] waiting")) {
chrome.runtime.sendMessage({state: "Pause"});
extensionIds.forEach(id =>
chrome.runtime.sendMessage(id, {state: "Pause"})
)
} else if (event.includes("[Audio] pause") || event.includes("[Audio] ended") || event.includes("[Audio] abort")) {
chrome.runtime.sendMessage({state: "Play"});
extensionIds.forEach(id =>
chrome.runtime.sendMessage(id, {state: "Play"})
)
}
}, false);

function listenLog() {
var originalLog = console.log;
let originalLog = console.log;
console.log = function (e) {
originalLog(e);
window.dispatchEvent(new CustomEvent("LogEvent", { detail: e }));
window.dispatchEvent(new CustomEvent("LogEvent", {detail: e}));
}
}

var script = document.createElement('script');
script.appendChild(document.createTextNode('(' + listenLog + ')();'));
(document.body || document.head || document.documentElement).appendChild(script);
listenLog()
}
53 changes: 33 additions & 20 deletions source/manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "__MSG_ext_name__",
"short_name": "__MSG_ext_short_name__",
"description": "__MSG_ext_description__",
"version": "2.0.15",
"version": "2.1.0",
"author": "Artem Zanin",
"default_locale": "en",
"icons": {
Expand All @@ -12,33 +12,45 @@
"128": "images/icon128.png"
},
"options_ui": {
"page": "options.html",
"chrome_style": true
"page": "options.html"
},
"permissions": [
"storage",
"tabs",
"scripting"
],
"host_permissions": [
"https://play.pocketcasts.com/*/"
],
"background": {
"persistent": false,
"scripts": [
"background.js"
]
"service_worker": "background.js"
},
"content_scripts": [{
"content_scripts": [
{
"matches": [
"https://play.pocketcasts.com/*/"
],
"js": [
"action-play.js",
"action-skip.js",
"log-listener-injector.js"
]
}
],
"web_accessible_resources": [
{
"resources": [
"log-listener.js"
],
"matches": [
"https://*/*"
]
}
],
"externally_connectable": {
"matches": [
"https://play.pocketcasts.com/*/"
],
"js": [
"action-play.js",
"action-skip.js",
"log-listener.js"
"*://*.pocketcasts.com/*"
]
}],
"browser_action": {
"default_title": "__MSG_Play__",
"default_icon": "images/Play.png"
},
"commands": {
"play-pause": {
Expand All @@ -62,5 +74,6 @@
"description": "__MSG_jump_forward__",
"global": true
}
}
},
"action": {}
}
Loading

0 comments on commit ded65f4

Please sign in to comment.