-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
62 lines (50 loc) · 1.64 KB
/
background.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
chrome.runtime.onInstalled.addListener(function () {
setUpContextMenus();
setText();
});
function setUpContextMenus() {
chrome.contextMenus.create({
title: 'Ban this shit',
id: 'BanUser',
contexts: ['selection']
});
}
chrome.contextMenus.onClicked.addListener(onClickHandler);
function onClickHandler(info, tab) {
//get group id
var groupId = info.pageUrl.match(/(\d+)/g)[0];
//get user id
callAjax(info.linkUrl, function (res) {
var userIdString = res.match(/fb:\/\/profile\/(\d+)/g)[0];
var userId = userIdString.replace('fb://profile/', '');
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: 'ban', groupId: groupId, userId: userId}, function (response) {
});
});
});
};
function callAjax(url, callback) {
var xmlhttp;
// compatible with IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function setText() {
chrome.storage.sync.get("numBan", function (data) {
var numBan = data.numBan;
chrome.browserAction.setBadgeText({text: numBan.toString()});
chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
});
}
chrome.runtime.onMessage.addListener(
function (req, sender, sendResponse) {
if (req.action == 'badgetText') {
setText();
}
});