Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update init.js removing only one instance playing kludge #2356

Merged
merged 10 commits into from
Jun 11, 2024
170 changes: 79 additions & 91 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,110 +210,98 @@ chrome.windows.onFocusChanged.addListener(function (windowId) {
/*--------------------------------------------------------------
# MESSAGE LISTENER
--------------------------------------------------------------*/
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
var name = request.name;
let tabConnected = {};

if (name === 'download') {
chrome.permissions.request({
permissions: ['downloads'],
origins: ['https://www.youtube.com/*']
}, function (granted) {
if (granted) {
try {
var blob = new Blob([JSON.stringify(request.value)], {
type: 'application/json;charset=utf-8'
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(message);
console.log(sender);

chrome.downloads.download({
url: URL.createObjectURL(blob),
filename: request.filename,
saveAs: true
});
} catch (error) {
console.error(error);
switch(message.action || message.name || message) {
case 'play':
chrome.tabs.query({ url: 'https://www.youtube.com/*' }).then(function (tabs) {
let tabIds = [];
for (let tab of tabs) {
tabIds.push(tab.id);
if (!tab.discarded && tab.id !== sender.tab.id && tabConnected[tab.id]) {
chrome.tabs.sendMessage(tab.id, {action: "another-video-started-playing"});
}
}
} else {
console.error('Permission is not granted.');
}
});
}
});
// prune stale tab-connected data
for (let id in tabConnected) {
if (!tabIds.includes(Number(id))) {
delete tabConnected[id];
}
}
}, function () {console.log("Error querying Tabs")});
break

let prevTabsLength = 0;
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var action = message.action || message;
case 'options-page-connected':
sendResponse({
isTab: sender.hasOwnProperty('tab')
});
break

if (action === "play") {
chrome.tabs.query({}, function (tabs) {
if (tabs.length > prevTabsLength) {
prevTabsLength = tabs.length;
for (let i = 0, l = tabs.length; i < l; i++) {
let tab = tabs[i];
chrome.tabs.sendMessage(tab.id, {action: "new-tab-opened"});
}
} else {
prevTabsLength = tabs.length;
}
for (let i = 0, l = tabs.length; i < l; i++) {
let tab = tabs[i];
case 'tab-connected':
tabConnected[sender.tab.id] = true;
sendResponse({
tabId: sender.tab.id
});
break

if (sender.tab.id !== tab.id) {
chrome.tabs.sendMessage(tab.id, {action: "another-video-started-playing"});
}
}
});
} else if (action === 'options-page-connected') {
sendResponse({
isTab: sender.hasOwnProperty('tab')
});
} else if (action === 'tab-connected') {
try{ sendResponse({
hostname: new URL(sender.url).hostname,
tabId: sender.tab.id
}); } catch (error) { console.error("invalid url?", error); }
} else if (action === 'fixPopup') {
case 'fixPopup':
//~ get the current focused tab and convert it to a URL-less popup (with same state and size)
chrome.windows.getLastFocused(w => {
chrome.tabs.query({
windowId: w.id,
active: true
}, ts => {
const tID = ts[0]?.id,
data = { type: 'popup',
state: w.state,
width: parseInt(message.width, 10),
height: parseInt(message.height, 10),
left: 0,
top: 20
}
chrome.windows.getLastFocused(w => {
chrome.tabs.query({
windowId: w.id,
active: true
}, ts => {
const tID = ts[0]?.id,
data = { type: 'popup',
state: w.state,
width: parseInt(message.width, 10),
height: parseInt(message.height, 10),
left: 0,
top: 20
}

if (tID) {data.tabId = tID;}
chrome.windows.create(data, pw => {});
if (tID) {data.tabId = tID;}
chrome.windows.create(data, pw => {});

//append to title?
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === tID && changeInfo.status === 'complete' && !message.title.startsWith("undefined")) {
chrome.tabs.onUpdated.removeListener(listener);
chrome.scripting.executeScript({ target: { tabId: tID }, func: () => { document.title = `${message.title} - ImprovedTube`; } }) //manifest3
// chrome.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`}); //manifest2
}
//append to title?
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === tID && changeInfo.status === 'complete' && !message.title.startsWith("undefined")) {
chrome.tabs.onUpdated.removeListener(listener);
chrome.scripting.executeScript({ target: { tabId: tID }, func: () => { document.title = `${message.title} - ImprovedTube`; } }); //manifest3
// chrome.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`}); //manifest2
}
});
});
});
});
break
case 'download':
chrome.permissions.request({
permissions: ['downloads'],
origins: ['https://www.youtube.com/*']
}, function (granted) {
if (granted) {
try {
var blob = new Blob([JSON.stringify(request.value)], {
type: 'application/json;charset=utf-8'
});
chrome.downloads.download({
url: URL.createObjectURL(blob),
filename: request.filename,
saveAs: true
});
} catch (error) {
console.error(error);
} else {
console.error('Permission is not granted.');
}
})
break
}
});
/*------ search results in new tab ---------
chrome.storage.local.get('open_new_tab', function (result)
{if (result.open_new_tab === true){

chrome.runtime.onMessage.addListener(function (request) {
if (request.action === "createNewTab") {
chrome.tabs.create({ url: request.url });
}
});

}}); */

/*-----# UNINSTALL URL-----------------------------------*/
chrome.runtime.setUninstallURL('https://improvedtube.com/uninstalled');

Expand Down
16 changes: 4 additions & 12 deletions js&css/extension/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ extension.inject([
'/js&css/web-accessible/www.youtube.com/shortcuts.js',
'/js&css/web-accessible/www.youtube.com/blocklist.js',
'/js&css/web-accessible/www.youtube.com/settings.js',
'/js&css/web-accessible/init.js',
'/js&css/web-accessible/mutations.js'
'/js&css/web-accessible/init.js'
], function () {
extension.ready = true;

Expand All @@ -79,7 +78,7 @@ document.addEventListener('DOMContentLoaded', function () {

bodyReady();
});
let prevRequestAction = "";

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'focus') {
extension.messages.send({
Expand Down Expand Up @@ -111,16 +110,9 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
extension.messages.send({
deleteCookies: true
});
} else if (
request.action === "another-video-started-playing" &&
prevRequestAction === "new-tab-opened"
) {
console.log("Continue playing");
} else if (request.action === "another-video-started-playing") {
} else if (request.action === "another-video-started-playing") {
extension.features.onlyOnePlayerInstancePlaying();
}

prevRequestAction = request.action;
}
});

document.addEventListener('it-message-from-youtube', function () {
Expand Down
22 changes: 0 additions & 22 deletions js&css/web-accessible/mutations.js

This file was deleted.

4 changes: 4 additions & 0 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ ImprovedTube.autoplayDisable = function (videoElement) {
setTimeout(function() { try { player.pauseVideo(); }
catch (error) { console.log("autoplayDisable: Pausing"); videoElement.pause(); }
});
} else {
document.dispatchEvent(new CustomEvent('it-play'));
}
} else {
document.dispatchEvent(new CustomEvent('it-play'));
}
};
/*------------------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"run_at": "document_start"
}
],
"host_permissions": [ "https://www.youtube.com/*" ],
"offline_enabled": true,
"optional_permissions": [
"downloads"
Expand All @@ -76,8 +77,7 @@
"js&css/web-accessible/www.youtube.com/shortcuts.js",
"js&css/web-accessible/www.youtube.com/blocklist.js",
"js&css/web-accessible/www.youtube.com/settings.js",
"js&css/web-accessible/init.js",
"js&css/web-accessible/mutations.js",
"js&css/web-accessible/init.js"
"stuff/icons/48.png"
],
"matches": [
Expand All @@ -86,4 +86,3 @@
}
]
}

5 changes: 2 additions & 3 deletions manifest2.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"run_at": "document_start"
}
],
"host_permissions": [ "https://www.youtube.com/*" ],
"offline_enabled": true,
"optional_permissions": [
"downloads"
Expand All @@ -77,8 +78,6 @@
"js&css/web-accessible/www.youtube.com/shortcuts.js",
"js&css/web-accessible/www.youtube.com/blocklist.js",
"js&css/web-accessible/www.youtube.com/settings.js",
"js&css/web-accessible/init.js",
"js&css/web-accessible/mutations.js"
"js&css/web-accessible/init.js"
]
}

Loading