Skip to content

Commit 5dd5243

Browse files
committed
Activation slider
1 parent 1b57dab commit 5dd5243

File tree

6 files changed

+161
-28
lines changed

6 files changed

+161
-28
lines changed

host/host-script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pull(fromWebExt, serverStream, toWebExt)*/
110110

111111
Client((err, sbot) => {
112112
if (err) {
113-
console.error("could not connect to existing server instance, starting new one", err)
113+
console.error("could not connect to ssb-server instance", err)
114114
return
115115
}
116116
sbot.manifest().then(manifest => {

src/background-script.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@ function notify(message) {
1010
if (message.direction === "from-menu-script") {
1111
if (message.message.action === 'grant') {
1212
console.log('granting acces to', message.message.target);
13-
//TODO implement
13+
browser.storage.local.get({'granted': []}).then(({granted}) => {
14+
console.log('granted acces to', granted)
15+
Object.entries(unconnectedPorts).forEach(([key,p]) => {
16+
const relevantURL = p.sender.url.split('?')[0].split('#')[0]
17+
if (~granted.indexOf(relevantURL)) {
18+
connectPort(p)
19+
delete unconnectedPorts[p.sender.tab.id]
20+
}
21+
})
22+
})
1423
}
1524
}
1625
}
1726

18-
let ports = []
27+
let unconnectedPorts = []
1928

20-
browser.runtime.onConnect.addListener(function connected(p) {
21-
ports[p.sender.tab.id] = p
29+
browser.runtime.onConnect.addListener(async function connected(p) {
30+
console.log('connection from', p.sender.url)
31+
const relevantURL = p.sender.url.split('?')[0].split('#')[0]
32+
const { granted } = await browser.storage.local.get('granted')
33+
if (~granted.indexOf(relevantURL)) {
34+
connectPort(p)
35+
} else {
36+
unconnectedPorts[p.sender.tab.id] = p
37+
}
38+
})
39+
40+
function openLocalPort(p) {
41+
42+
}
43+
44+
function connectPort(p) {
2245
//p.sender.tab.onClose(() => console.log('tab closed, should close connection'))
2346
const [fromContentScript, toContentScript] = createConnection(p)
24-
const [fromNativeScript, toNativeScript] = createNativeConnection(p)
47+
const [fromNativeScript, toNativeScript] = createNativeConnection()
2548

2649
pull(
2750
fromContentScript,
@@ -33,7 +56,7 @@ browser.runtime.onConnect.addListener(function connected(p) {
3356
//logger('from native to content'),
3457
toContentScript
3558
)
36-
})
59+
}
3760

3861
function createNativeConnection() {
3962
const port = browser.runtime.connectNative("ssb4all")

webext/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"permissions": [
1313
"activeTab",
1414
"nativeMessaging",
15+
"storage",
1516
"<all_urls>"
1617
],
1718

webext/menu-script.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
console.log('menu opened')
2-
const menuArea = document.getElementById('options')
3-
console.log(document, menuArea)
2+
const toggleSwitch = document.getElementById('toggle')
3+
const locationArea = document.getElementById('location')
44
// this doesn't work: browser.tabs.getCurrent().then(...
55
browser.tabs.query({ "active": true, "currentWindow": true, "windowType": "normal" }).then(
66
tabs => tabs[0]
77
).then(
88
tab => {
99
console.log('tab', tab)
1010
const relevantURL = tab.url.split('?')[0].split('#')[0]
11-
menuArea.innerHTML += `Allow the page at ${relevantURL} to access SSB?`
12-
const button = document.createElement('button')
13-
button.innerText = 'Grant'
14-
button.addEventListener('click', () => {
15-
16-
browser.runtime.sendMessage({
17-
direction: "from-menu-script",
18-
message: {
19-
action: 'grant',
20-
target: relevantURL
11+
locationArea.innerText = relevantURL
12+
browser.storage.local.get('granted').then(result => {
13+
let granted = result.granted
14+
const enabled = !!~granted.indexOf(relevantURL)
15+
console.log('enabled =', enabled)
16+
toggleSwitch.checked = enabled
17+
toggleSwitch.addEventListener('change', (e) => {
18+
console.log('changing', relevantURL, enabled, e.target.checked)
19+
if (e.target.checked) {
20+
granted.push(relevantURL)
21+
} else {
22+
granted = granted.filter(e => e !== relevantURL)
2123
}
22-
});
24+
browser.storage.local.set({'granted': granted}).then(() => {
25+
return browser.runtime.sendMessage({
26+
direction: "from-menu-script",
27+
message: {
28+
action: e.target.checked ? 'grant' : 'revoke',
29+
target: relevantURL
30+
}
31+
})
32+
}).then(() => { window.setTimeout(window.close, 500) })
33+
})
2334
})
24-
menuArea.appendChild(button)
2535
},
2636
error => {
2737
console.log('error', error)
28-
})
38+
}
39+
)
40+
41+

webext/menu.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
<!DOCTYPE html>
12
<html>
2-
<head>
3-
4-
</head>
3+
4+
<head>
5+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
6+
<meta content="utf-8" http-equiv="encoding">
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
510
<body>
6-
<h1>SSBversive</h1>
7-
<div id="options"></div>
8-
<script src="menu-script.js" ></script>
11+
<h1>SSBversive</h1>
12+
<div class="grid">
13+
<div class="left bold">SSB access on:</div>
14+
<div class="left" id="location">...</div>
15+
<div class="right">
16+
<label class="switch">
17+
<input id="toggle" type="checkbox">
18+
<span class="slider"></span>
19+
</label>
20+
</div>
21+
</div>
22+
<script src="menu-script.js"></script>
923
</body>
24+
1025
</html>

webext/style.css

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
.bold {
2+
font-weight: bold;
3+
}
4+
5+
.grid {
6+
display: grid;
7+
gap: 4px;
8+
grid-template-columns: auto 68px;
9+
}
10+
11+
.left {
12+
grid-column: 1;
13+
}
14+
15+
.right {
16+
grid-column: 2;
17+
grid-row: 1 / span 2;
18+
display: flex;
19+
flex-direction: column;
20+
justify-content: center;
21+
}
22+
23+
/* The switch - the box around the slider */
24+
.switch {
25+
position: relative;
26+
display: block;
27+
width: 68px;
28+
height: 20px;
29+
}
30+
31+
/* Hide default HTML checkbox */
32+
.switch input {
33+
opacity: 0;
34+
width: 100%;
35+
height: 0;
36+
}
37+
38+
/* The slider */
39+
.slider {
40+
position: absolute;
41+
cursor: pointer;
42+
top: 0;
43+
left: 0;
44+
right: 0;
45+
bottom: 0;
46+
background-color: #d1c4e9;
47+
-webkit-transition: background-color .4s;
48+
transition: background-color .4s;
49+
}
50+
51+
.slider:before {
52+
position: absolute;
53+
content: "OFF";
54+
font-size: 11px;
55+
text-align: center;
56+
line-height: 16px;
57+
height: 16px;
58+
width: 32px;
59+
top: 2px;
60+
left: 2px;
61+
bottom: 2px;
62+
background-color: white;
63+
-webkit-transition: .4s;
64+
transition: .4s;
65+
}
66+
67+
input:checked + .slider {
68+
background-color: #4caf50;
69+
}
70+
71+
input:focus + .slider {
72+
/* box-shadow: 0 0 1px #2196F3; */
73+
outline: 1px solid;
74+
}
75+
76+
input:checked + .slider:before {
77+
content: "ON";
78+
-webkit-transform: translateX(32px);
79+
-ms-transform: translateX(32px);
80+
transform: translateX(32px);
81+
}

0 commit comments

Comments
 (0)