-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
36 lines (36 loc) · 1007 Bytes
/
helpers.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
/**
* Inserts code into given tab.
* @param tabId The id corresponding to the tab to insert into.
* @param css A string containing CSS code to inject.
* @param js A string containing Javascript code to inject.
*/
function insertCode(tabId, css, js) {
if(typeof css == "string") chrome.tabs.insertCSS(tabId, {code:css});
if(typeof js == "string") chrome.tabs.executeScript(tabId, {code:js});
}
/**
* Retrieves just the hostname of a given tab.
* @param tab The tab from which to extract the hostname.
* @return A string containing the hostname for the given tab.
*/
function tab2domain(tab) {
var myregexp = /[a-z][a-z0-9+\-.]*:\/\/(www\.)?([a-z0-9\-_.~%]*)/i;
var match = myregexp.exec(tab.url);
if (match != null) {
result = match[2];
} else {
result = false;
}
return result;
}
function removeItem(array, item) {
var i = 0;
while (i < array.length) {
if (array[i] == item) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}