-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
94 lines (84 loc) · 2.54 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
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
'use strict';
let scopedTabId = 0;
// dat burp doe.
const config = {
mode: 'fixed_servers',
rules: {
singleProxy: {
host: '127.0.0.1',
port: 8080
}
}
};
// lol
function addTabToScope ( tabId ) {
scopedTabId = tabId;
}
// lol2
function removeTabFromScope ( tabId ) {
scopedTabId = 0;
}
// generate an osd to notify when intercepting
function osd ( tabId ) {
let template = scopedTabId ? `Started intercepting tab: ${tabId}` : `Stopped intercepted tab: ${tabId}`;
let notificationId = Math.random().toString(36).substring(0, 12);
chrome.notifications.create( notificationId, {
type : 'basic',
title : template,
message : '',
iconUrl : 'icons/test-48.png',
}, () => {
// clear notifications once they are up so that status can be tracked quicker.
setTimeout(() => {
chrome.notifications.clear( notificationId );
}, 500);
}
);
}
// dem hotkeys doe.
chrome.commands.onCommand.addListener(
( command ) => {
switch ( command ) {
// ctrl + shift + i
// cmd + shift + i
case 'toggle-proxy':
// get the current tab's object
chrome.tabs.query({ currentWindow: true, active: true },
( currentTab ) => {
if ( !currentTab.length ) return 0; // if it's not a real tab return to 'exit'
let tabId = currentTab[0].id;
scopedTabId === 0 ? addTabToScope( tabId ) : removeTabFromScope( tabId ); // toggle add/remove scope
// create osd
osd( tabId );
}
);
break;
}
}
);
// on tab close
chrome.tabs.onRemoved.addListener(
( tabId, removed ) => {
// if a scoped tab was closed we remove scope
if ( tabId === scopedTabId ) {
scopedTabId = 0;
}
}
);
// triggers on each web request
chrome.webRequest.onBeforeRequest.addListener( function ( details ) {
// capture initial request if it originates from tabId -1 and a scope is set
if ( !details.tabId && scopedTabId ) {
chrome.proxy.settings.set({ value: config, scope: 'regular' }, () => {
return { cancel: false }; // allow request
});
} else if ( details.tabId !== scopedTabId ) { // clear proxy settings if not a scopedTab
chrome.proxy.settings.clear({ scope: 'regular' }, () => {
return { cancel: false }; // allow request
});
} else { // proxy if scopedTab
chrome.proxy.settings.set({ value: config, scope: 'regular' }, () => {
return { cancel: false }; // allow request
});
}
}, {urls: ["<all_urls>"]}, ["blocking"]);