-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
190 lines (173 loc) · 5.2 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
function updateLanguage(language) {
switch (language) {
case 'english':
document.querySelector('#group').innerHTML = 'Group matching tabs';
document.querySelector('#ungroup').innerHTML = 'Ungroup all tabs';
document.querySelector('#lang').innerHTML = 'عربي';
break;
case 'arabic':
document.querySelector('#group').innerHTML = 'تجميع علامات التبويب المطابقة';
document.querySelector('#ungroup').innerHTML = 'فك تجميع كل علامات تبويب';
document.querySelector('#lang').innerHTML = 'English';
break;
}
}
// console.log('hi');
function getHostName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
var hostname = match[2].split('.');
return hostname[0];
} else {
return null;
}
}
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for (var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for (i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
const groupMatching = () => {
chrome.tabs.query(
{
currentWindow: true,
},
(tabs) => {
let map = new Map();
// let domains = tabs.map((tab) => new URL(tab.url).hostname);
// tabs.map((tab) => {
// let domain = new URL(tab.url).hostname;
// map.set(domain, (map.get(domain) || 0) + 1);
// });
tabs.map((tab) => {
// tab.domain = new URL(tab.url).hostname;
let domain = new URL(tab.url).hostname;
domain = domain
.split('.')
.filter((x) => x != 'www')
.join('.');
console.log(domain);
tab.domain = domain;
});
tabs.forEach((a) => map.set(a.domain, (map.get(a.domain) || 0) + 1));
let grouped = tabs.filter((a) => map.get(a.domain) > 1);
let domains = removeDuplicates(grouped, 'domain');
domains.map((tab) => {
let children = grouped.filter((x) => x.domain == tab.domain);
// console.log(tab.domain);
// console.log(children.some((x) => x.active));
// children.map((child) => {
// if (child.pinned) {
// child.wasPinned = true;
// }
// });
// console.log(children);
chrome.tabs.group(
{
tabIds: children.map((x) => x.id),
},
(id) => {
// console.log(id);
chrome.tabGroups.update(id, {
collapsed: !children.some((x) => x.active),
title: tab.domain,
});
}
);
// console.log(tab.domain);
// console.log(children);
});
// let groups = [];
// console.log(grouped);
// console.log(domains);
// for(let i = 0; i < domains.length; i++) {
// let domains[i]
// }
// console.log(domains);
// obj.arr = obj.arr.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
// console.log(uniq(domains, 'url'));
// console.log();
}
);
// console.log('bye');
};
const ungroupAll = () => {
chrome.tabs.query(
{
currentWindow: true,
},
(tabs) => {
chrome.tabs.ungroup(
tabs.map((x) => x.id),
// () => {
// tabs.map((x) => {
// console.log(x);
// if (x.wasPinned) {
// chrome.tabs.update(x.id, { pinned: true });
// x.wasPinned = false;
// }
// });
// // console.log('ungrouped');
// // // var updateProperties = { active: true };
// // chrome.tabs.query({ currentWindow: true }, (tabs) => {
// // console.log(tabs[6].id);
// // chrome.tabs.highlight({ tabs: tabs[6].index }, function () {});
// // // chrome.tabs.update(tabs[4].id, { active: true }, (tab) => {});
// // });
// }
() => {
// chrome.tabs.query({ currentWindow: true, index: tabs.length - 1 }, ([tab]) => {
// for (var i = arr.length - 1; i >= 0; i--) {
// console.log(arr[i]);
// }
// chrome.tabs.move(tab.id, { index: 0 }, () => {});
// });
}
);
}
);
};
let buttonToLang = {
عربي: 'arabic',
English: 'english',
};
// get language setting
chrome.storage.local.get(['language'], (result) => {
if (result.language == undefined) {
// if there is none (first time opening extension), set it to english
chrome.storage.local.set({ language: 'english' });
} else {
// update button text to language
updateLanguage(result.language);
}
});
// chrome.storage.local.set({key: value}, function() {
// console.log('Value is set to ' + value);
// });
// chrome.storage.local.get(['key'], function(result) {
// console.log('Value currently is ' + result.key);
// });
// updateLanguage
document.querySelector('#group').addEventListener('click', groupMatching);
document.querySelector('#ungroup').addEventListener('click', ungroupAll);
document.querySelector('#lang').addEventListener('click', () => {
// console.log(document.querySelector('#lang').innerHTML);
let newLang = buttonToLang[document.querySelector('#lang').innerHTML];
// console.log(newLang);
updateLanguage(newLang);
// save to storage
chrome.storage.local.set({ language: newLang });
});
// chrome.action.onClicked.addListener((tab) => {
// chrome.tabs.create({ url: 'http://www.stackoverflow.com' });
// // chrome.scripting.executeScript({
// // target: {tabId: tab.id},
// // files: ['content.js']
// // });
// });