Skip to content

Commit

Permalink
Merge pull request #2121 from raszpl/patch-6
Browse files Browse the repository at this point in the history
background.js hideSubscribe only once
  • Loading branch information
ImprovedTube authored Mar 25, 2024
2 parents 3676cd1 + 982ecc6 commit f935ebf
Showing 1 changed file with 107 additions and 111 deletions.
218 changes: 107 additions & 111 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,73 @@
/*
// For Manifest3:
/*-----# Persistent Serviceworker:
"Manifest2 Background.js"-----*/
// Periodic "keep-alive" message every 29.5 seconds
"Manifest2 Background.js"-----*/
// Periodic "keep-alive" message every 29.5 seconds
// const keepAliveInterval = setInterval(() => chrome.runtime.sendMessage({ status: 'keep-alive' }), 29.5 * 1000);

/* Sidepanel Option */
/* Sidepanel Option */
/*
chrome.storage.local.get('improvedTubeSidebar', function (result) {
if ( result.improvedTubeSidebar) { if ( result.ImprovedTubeSidebar === true) {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true })
} } else {chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: false }) }
});
*/
*/
/*---------------------------
# IMPORTING OLD SETTINGS
-----------------------------*/
chrome.runtime.onInstalled.addListener(function (installed){
if(installed.reason == 'update'){
// var thisVersion = chrome.runtime.getManifest().version;
// console.log("Updated from " + installed.previousVersion + " to " + thisVersion + "!");
chrome.storage.local.get('channel_default_tab', function (result) {if (result.channel_default_tab === '/home'){ chrome.storage.local.set({channel_default_tab: '/'}); }});
chrome.storage.local.get('hideSubscribe', function (result) {if (result.hideSubscribe === true){ chrome.storage.local.set({subscribe: 'hidden'}); }});
chrome.storage.local.get('limit_page_width', function (result) {
if (result.limit_page_width === false){
chrome.storage.local.set({no_page_margin: true});
chrome.storage.local.remove(['limit_page_width'], (i) => {});
chrome.storage.local.get('player_size', function (r) {
if (r.player_size == 'full_window' || 'fit_to_window') {
chrome.storage.local.set({player_size: 'max_width'});
}});
}
});
}
else if(installed.reason == 'install'){
if(navigator.userAgent.indexOf("Firefox") != -1){chrome.storage.local.set({below_player_pip: false})};
if(navigator.userAgent.indexOf("Safari") != -1){chrome.storage.local.set({below_player_pip: false})};
-----------------------------*/
chrome.runtime.onInstalled.addListener(function (installed) {
if(installed.reason == 'update') {
// var thisVersion = chrome.runtime.getManifest().version;
// console.log("Updated from " + installed.previousVersion + " to " + thisVersion + "!");
chrome.storage.local.get('channel_default_tab', function (result) {
if (result.channel_default_tab === '/home') {
chrome.storage.local.set({channel_default_tab: '/'});
}
});
chrome.storage.local.get('hideSubscribe', function (result) {
if (result.hideSubscribe === true) {
chrome.storage.local.set({subscribe: 'hidden'});
chrome.storage.local.remove(['hideSubscribe'], (i) => {});
}
});
chrome.storage.local.get('limit_page_width', function (result) {
if (result.limit_page_width === false) {
chrome.storage.local.set({no_page_margin: true});
chrome.storage.local.remove(['limit_page_width'], (i) => {});
chrome.storage.local.get('player_size', function (r) {
if (r.player_size == 'full_window' || 'fit_to_window') {
chrome.storage.local.set({player_size: 'max_width'});
}
});
}
});
} else if(installed.reason == 'install') {
if(navigator.userAgent.indexOf("Firefox") != -1) {chrome.storage.local.set({below_player_pip: false})}
if(navigator.userAgent.indexOf("Safari") != -1) {chrome.storage.local.set({below_player_pip: false})}

// still needed? (are screenshots broken in Safari?):
if(navigator.userAgent.indexOf("Safari") != -1){chrome.storage.local.set({below_player_screenshot: false})};
// console.log('Thanks for installing!');
};
}
);
// still needed? (are screenshots broken in Safari?):
if(navigator.userAgent.indexOf("Safari") != -1) {chrome.storage.local.set({below_player_screenshot: false})}
// console.log('Thanks for installing!');
}
});
/*--------------------------------------------------------------
# LOCALE
--------------------------------------------------------------*/
function getLocale(language, callback) {
language = language.replace('-', '_');
fetch('_locales/' + language.substring(0,2) + '/messages.json').then(function (response) {
if (response.ok) {response.json().then(callback);
} else {
fetch('_locales/' + language.substring(0,2) + '/messages.json').then(function (response) {
if (response.ok) { response.json().then(callback);
} else {getLocale('en', callback); } }).catch(function (){getLocale('en', callback);
});
if (response.ok) {
response.json().then(callback);
} else {
fetch('_locales/' + language.substring(0,2) + '/messages.json').then(function (response) {
if (response.ok) {
response.json().then(callback);
} else {
getLocale('en', callback);
}
}).catch(function () { getLocale('en', callback); });
getLocale('en', callback);
}
}).catch(function () {
Expand Down Expand Up @@ -101,7 +113,7 @@ function updateContextMenu(language) {
chrome.contextMenus.create({
id: String(i),
title: text,
// contexts: ['action'] //manifest3
// contexts: ['action'] //manifest3
contexts: ['browser_action'] //manifest2
});
}
Expand All @@ -125,27 +137,23 @@ chrome.runtime.onInstalled.addListener(function (details) {

chrome.storage.onChanged.addListener(function (changes) {
for (var key in changes) {
if (key === 'language') {updateContextMenu(changes[key].newValue);}
if (key === 'improvedTubeSidebar') { chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: changes[key].newValue }); }
if (key === 'language') { updateContextMenu(changes[key].newValue); }
if (key === 'improvedTubeSidebar') { chrome.sidePanel.setPanelBehavior({openPanelOnActionClick: changes[key].newValue}); }
}
});
/*--------------------------------------------------------------
# TAB FOCUS/BLUR
--------------------------------------------------------------*/
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.sendMessage(activeInfo.tabId, {
action: 'focus'
});
chrome.tabs.sendMessage(activeInfo.tabId, {action: 'focus'});

chrome.tabs.query({
windowId: activeInfo.windowId
}, function (tabs) {
if (tabs) {
for (var i = 0, l = tabs.length; i < l; i++) {
if (tabs[i].id !== activeInfo.tabId) {
chrome.tabs.sendMessage(tabs[i].id, {
action: 'blur'
});
chrome.tabs.sendMessage(tabs[i].id, {action: 'blur'});
}
}
}
Expand All @@ -163,24 +171,18 @@ chrome.windows.onFocusChanged.addListener(function (windowId) {
var tab = tabs[j];

if (tab.active) {
chrome.tabs.sendMessage(tab.id, {
action: 'focus'
});
chrome.tabs.sendMessage(tab.id, {action: 'focus'});
}
}
}
});
} else {
chrome.tabs.query({
windowId: windows[i].id
}, function (tabs) {
chrome.tabs.query({windowId: windows[i].id}, function (tabs) {
if (tabs) {
for (var j = 0, k = tabs.length; j < k; j++) {
var tab = tabs[j];

chrome.tabs.sendMessage(tab.id, {
action: 'blur'
});
chrome.tabs.sendMessage(tab.id, {action: 'blur'});
}
}
});
Expand Down Expand Up @@ -222,75 +224,69 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {

let prevTabsLength = 0;
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var action = message.action || message;
var action = message.action || message;

if (action === "play") {
chrome.tabs.query({}, function (tabs) {
if (tabs.length > prevTabsLength) {
prevTabsLength = tabs.length;
for (var i = 0, l = tabs.length; i < l; i++) {
var tab = tabs[i];
chrome.tabs.sendMessage(tab.id, {
action: "new-tab-opened",
});
}
} else {
prevTabsLength = tabs.length;
}
for (var i = 0, l = tabs.length; i < l; i++) {
var tab = tabs[i];
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];

if (sender.tab.id !== tab.id) {
chrome.tabs.sendMessage(tab.id, {
action: "another-video-started-playing",
});
}
}
});
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({
});
} 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') {
}); } catch (error) { console.error("invalid url?", error); }
} else if (action === '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
};
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.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`});
}});

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 => {});

//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.tabs.executeScript(tID, {code: `document.title = "${message.title} - ImprovedTube";`});
}
});
});
});


};
}
});
/*------ search results in new tab ---------
chrome.storage.local.get('open_new_tab', function (result)
{if (result.open_new_tab === true){
/*------ 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") {
Expand All @@ -299,6 +295,6 @@ chrome.runtime.onMessage.addListener(function (request) {
});
}}); */

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

0 comments on commit f935ebf

Please sign in to comment.