Skip to content

Instantly share code, notes, and snippets.

@Orochimarufan
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orochimarufan/9e3b69b2d093dcb5c0a3 to your computer and use it in GitHub Desktop.
Save Orochimarufan/9e3b69b2d093dcb5c0a3 to your computer and use it in GitHub Desktop.
FooTab extension
// Copyright (c) 2013 mike_sf@outlook.com
// 2015 Taeyeon Mori <orochimarufan@vivaldi.net>
// All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const ftTimeout = 10000;
var activated = null; //< Active tab: "t<tabId>"
// Reload activated tab if it's been held back
function ftLoadTab(tabId)
{
ftExecSetTabState(
tabId,
false,
function(heldBack)
{
if (heldBack)
chrome.tabs.reload(tabId);
}
);
}
function ftExecSetTabState(tabId, newHeldBackState, callback)
{
var key = "t" + tabId;
chrome.storage.local.get([key, "count"], function(stor)
{
if ((stor[key] && !newHeldBackState) || (!stor[key] && newHeldBackState))
{
var count = stor["count"] || 0;
if (newHeldBackState)
chrome.storage.local.set({key: true, "count": ++count});
else
{
chrome.storage.local.remove(key);
chrome.storage.local.set({"count": --count});
}
if (count < 1)
ftKill();
}
callback(stor[key]);
});
}
// Hook tab switching
function ftTabActivated(info) {
// console.log(info.tabId+' act: '+active);
activated = info.tabId;
ftLoadTab(info.tabId);
}
chrome.tabs.onActivated.addListener(ftTabActivated);
function ftTabUpdated(tabId, changeInfo, tab) {
// console.log(tabId+' upd: '+active+' stat: '+changeInfo.status+' act: '+tab.active);
if (changeInfo.status == 'complete' && tab.active)
{
activated = tabId;
ftLoadTab(tabId);
}
}
chrome.tabs.onUpdated.addListener(ftTabUpdated);
// Block all requests from non-active tabs until ftTimeout expires
function ftResetTimeout()
{
setTimeout(ftDisable, ftTimeout);
}
function ftBlockRequest(info) {
// console.log(info.tabId+' webrq: '+active);
ftResetTimeout();
if (info.tabId == activated)
return;
else
ftExecSetTabState(info.tabId, true);
return {cancel: true};
}
chrome.webRequest.onBeforeRequest.addListener(
ftBlockRequest,
// filters
{
urls: ["*://*/*"],
//types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "other"]
},
// extraInfoSpec
["blocking"]
);
// Reset timeout when new tab is created
function ftInstallInWindow(window)
{
ftResetTimeout();
}
chrome.windows.onCreated.addListener(ftInstallInWindow);
// Disable and Kill it
function ftDisable()
{
chrome.webRequest.onBeforeRequest.removeListener(ftBlockRequest);
chrome.windows.onCreated.removeListener(ftInstallInWindow);
}
function ftKill()
{
chrome.tabs.onUpdated.removeListener(ftTabUpdated);
chrome.tabs.onActivated.removeListener(ftTabActivated);
}
Copyright (c) 2013, mike_sf@outlook.com
2015, Taeyeon Mori <orochimarufan@vivaldi.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the mike_sf@outlook.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original Project at http://sourceforge.net/projects/footab/
Overhauled version at https://gist.github.com/Orochimarufan/9e3b69b2d093dcb5c0a3
{
"name": "FooTab",
"description": "BarTab inspired (but infinitely more primitive) utility for blocking background tabs from killing Vivaldi/Chrome on start.",
"version": "15.0",
"permissions": ["webRequest", "webRequestBlocking", "storage", "*://*/*"],
"background": {
"scripts": ["background.js"]
},
"icons": {"128": "footab128.png"},
"homepage_url": "https://gist.github.com/Orochimarufan/9e3b69b2d093dcb5c0a3",
"manifest_version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment