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

Multi window #13

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
106 changes: 70 additions & 36 deletions td.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,71 @@

chrome.browserAction.onClicked.addListener(closeDuplicateTabsInCurrentWindow);
chrome.browserAction.onClicked.addListener(closeDuplicateTabsInAllWindows);
chrome.tabs.onUpdated.addListener(countDuplicateSiblings);
chrome.tabs.onRemoved.addListener(countDuplicateSiblings);
chrome.tabs.onRemoved.addListener(countDuplicateSiblingsOnRemoved);

function closeDuplicateTabsInAllWindows()
{
chrome.windows.getAll(
{
"populate": true,
"windowTypes": ["normal"]
}, closeDuplicateTabs);
}

function closeDuplicateTabsInCurrentWindow()
function countDuplicateSiblings(tabId, changeInfo)
{
chrome.tabs.getAllInWindow(null, closeDuplicateTabs);
if (changeInfo.status === 'complete')
{
chrome.windows.getAll(
{
"populate": true,
"windowTypes": ["normal"]
}, countDuplicateTabs);
}
}

function countDuplicateSiblings(tab)
function countDuplicateSiblingsOnRemoved()
{
chrome.tabs.getAllInWindow(tab.windowId, countDuplicateTabs);
chrome.windows.getAll(
{
"populate": true,
"windowTypes": ["normal"]
}, countDuplicateTabs);
}

function closeDuplicateTabs(tabs)
function closeDuplicateTabs(windows)
{
let tabs = [];
for (let index in windows)
{
tabs.push(windows[index].tabs)
}
processDuplicates(tabs, new Closer());
updateDisplay(new Display());
}

function countDuplicateTabs(tabs)
function countDuplicateTabs(windows)
{
var counter = new Counter();
processDuplicates(tabs, counter);
updateDisplay(new Display(counter));
let tabs = [];
for (let index in windows)
{
tabs.push(windows[index].tabs)
}

const counter = new Counter();
processDuplicates(tabs, counter);
updateDisplay(new Display(counter));
}

function processDuplicates(tabs, implementation)
{
var processor = new DuplicateProcessor(implementation);
for (var index in tabs)
const processor = new DuplicateProcessor(implementation);

for (let index in tabs)
{
processor.process(tabs[index]);
for (let jIndex = 0; jIndex < tabs[index].length; jIndex++)
{
processor.process(tabs[index][jIndex]);
}
}
}

Expand All @@ -44,18 +78,16 @@ function updateDisplay(display)
function DuplicateProcessor(implementation)
{
this.cache = new TabCache();
this.implementation = implementation;
this.process = function(tab)
{
var found = this.cache.exists(tab);
if (found)
{
implementation.execute(this.nonSelected(found, tab));
}
else
{
this.cache.remember(tab);
}
const found = this.cache.exists(tab);

if (found)
{
implementation.execute(this.nonSelected(found, tab));
} else {
this.cache.remember(tab);
}
};

this.nonSelected = function(found, tab)
Expand All @@ -70,8 +102,9 @@ function DuplicateProcessor(implementation)
{
return tab;
}

return null;

// this seems to work just fine, but does seem quite right
return tab;
};
}

Expand All @@ -81,8 +114,8 @@ function Counter()
this.urls = "";
this.execute = function(tab)
{
this.count += 1;
this.urls += tab.url + '\n';
this.count += 1;
this.urls += tab.url + '\n';
};
}

Expand All @@ -109,18 +142,19 @@ function TabCache()
};
}

function Display()
{
this.title = "Tab Dupectomy";
this.text = "";
}

function Display(counter)
{
if (!counter)
{
this.title = "";
this.text = "";
return
}

this.title = counter.urls;
this.text = "";

if (counter.count != 0)
if (counter.count !== 0)
{
this.text = counter.count + '';
}
Expand Down