Skip to content

Commit b50dc67

Browse files
committed
save pc
1 parent fcaab81 commit b50dc67

File tree

18 files changed

+339
-120
lines changed

18 files changed

+339
-120
lines changed

config_files/browser_with_tabs.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
"enabled": true,
7272
"actions":
7373
{
74+
"webpage_filter":
75+
{
76+
"enabled": true,
77+
"hosts_whitelist": ["google"],
78+
"hosts_blacklist": [],
79+
"webpages_whitelist": [],
80+
"webpages_blacklist": [],
81+
"allow_navigation": true,
82+
"extension": "webpage-filter"
83+
},
7484
"toolbar":
7585
{
7686
"enabled": true,

extensions/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Loader2
8080
{
8181
try
8282
{
83+
if (!ext || !modules[ext]) return console.log(kleur.red("Error:"), ext, "is not a module!!");
8384
const ModuleClass = require(modules[ext]);
8485
if (typeof(ModuleClass) !== typeof(function () {}) || Object.getPrototypeOf(ModuleClass) !== BaseModule) { console.log(kleur.grey("Not loading " + ext + ": not a module")); return } ;
8586
const t = new ModuleClass()

extensions/splashscreen/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class Splashscreen extends require('../../lib/BaseModule')
4444
// setTimeout(TabsManager.setTab, 5000, mainTab);
4545
}
4646

47-
onNewTabCreated()
47+
onNewTabCreated(newTab)
4848
{
49-
this.tab.webContents.on('input-event', (e, input) => {
49+
newTab.webContents.on('input-event', (e, input) => {
5050
this.inputed = true;
5151
});
5252
this.log('listener set');

extensions/tab_browser/new_page.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

extensions/tab_browser/main.js renamed to extensions/toolbar/main.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ const fs = require('fs');
66
const icpChannel = require('../../lib/icpChannel');
77
const Env = require('../../env');
88
const { getPageTitle } = require('../../lib/utils');
9+
const BaseModule = require('../../lib/BaseModule');
10+
const kleur = require('kleur');
11+
const ipcChannel = require('../../lib/icpChannel');
912

1013
// Sample Module. Plase copy-paste this file into new module's main folder
11-
class Toolbar extends require('../../lib/BaseModule')
14+
class Toolbar extends BaseModule
1215
{
1316
MODULE_NAME = "toolbar"; // MUST be the same as the 'extension' field in config.json
1417
static toolbar_tab;
1518
tabs_count = 0;
1619
track_active_tab = false;
1720
first_avoided = false;
1821

19-
// required_modules = ['window-events'];
20-
2122
onNewTabCreated(new_tab, old_tab)
2223
{
2324
new_tab.webContents.setWindowOpenHandler((details) => {
@@ -33,48 +34,67 @@ class Toolbar extends require('../../lib/BaseModule')
3334
{
3435
if (this.__conf.hidden != true)
3536
{
36-
Toolbar.toolbar_tab = new WebContentsView({
37+
this.tab = new WebContentsView({
3738
webPreferences: {
3839
preload: path.join(__dirname, '../single_preload.js'),
3940
additionalArguments: ['--load-only=' + path.join(__dirname, 'toolbar_preload.js')],
4041
...Env.WEBVIEW_DEFAULT_PREFERENCES
4142
}});
43+
Toolbar.toolbar_tab = this.tab;
44+
this.tab.tab_id = 'toolbar';
4245

43-
this.tab = Toolbar.toolbar_tab;
44-
45-
Toolbar.toolbar_tab.webContents.loadURL(url.format({
46+
this.tab.webContents.loadURL(url.format({
4647
pathname: path.join(__dirname, this.__conf.toolbar_html),
4748
protocol: 'file'
4849
}));
4950

5051
this.warn({height: this.__conf.toolbar_width});
5152
if (this.__conf.default_url.protocol == 'file') this.__conf.default_url.pathname = path.join(__dirname, this.__conf.default_url.pathname);
52-
TabsManager.newSideTab(Toolbar.toolbar_tab, 'toolbar', {height: this.__conf.toolbar_width});
53+
TabsManager.newSideTab(this.tab, 'toolbar', {height: this.__conf.toolbar_width});
5354
TabsManager.newDefaultBounds({y: this.__conf.toolbar_width});
5455

55-
56-
this.newCtrlShortcut('r', () => { Toolbar.toolbar_tab.webContents.reload(); });
56+
this.newCtrlShortcut('r', () => { this.tab.webContents.reload(); });
5757

58-
fs.watch(__dirname, (event) => {if (event == "change") {Toolbar.toolbar_tab.webContents.reload(); this.log(this.__conf.toolbar_html, "has been modified! Reloading toolbar.")}});
58+
fs.watch(__dirname, (event) => {if (event == "change") {this.tab.webContents.reload(); this.log(this.__conf.toolbar_html, "has been modified! Reloading toolbar.")}});
5959

6060
icpChannel.newMainHandler('created-tab', (_, tab_url) => this.createNewTab(url.format(this.__conf.default_url)));
6161
icpChannel.newMainHandler('switch-tab', (_, index) => this.setActiveTab(index));
62-
icpChannel.newMainHandler('close-tab', (_, index) => this.closeTab(index));
62+
icpChannel.newMainHandler('closed-tab', (_, index) => this.closeTab(TabsManager.idToName(index)));
6363

64-
Toolbar.toolbar_tab.webContents.openDevTools({mode: 'detach'});
64+
this.tab.webContents.openDevTools({mode: 'detach'});
6565
}
6666
}
67-
67+
6868
late_setup()
6969
{
70+
this.tab.webContents.on('before-input-event', (event, input) => {
71+
if (input.key)
72+
{
73+
this.warn("forwarding event to active tab");
74+
TabsManager.getActiveTab().webContents.send('before-input-event', input);
75+
event.preventDefault();
76+
}
77+
});
7078
if (this.__conf.create_on_open == true)
7179
this.requestNewTab();
7280
}
7381

7482
// this method can be called from ANYWHERE (after initialization), thanks to the singleton behaviour of the modules.
7583
async requestNewTab(req_url = this.__conf.default_url)
7684
{
77-
icpChannel.sendSignalToRender('create-tab', Toolbar.toolbar_tab, await this.createNewTab(typeof(req_url) == 'string' ? req_url : url.format(req_url), true));
85+
icpChannel.sendSignalToRender('create-tab', this.tab, await this.createNewTab(typeof(req_url) == 'string' ? req_url : url.format(req_url), true));
86+
}
87+
88+
static requestCloseTabId(tab_id)
89+
{
90+
Toolbar.requestCloseTab(TabsManager.getNameTab(tab_id));
91+
}
92+
93+
static requestCloseTab(tab)
94+
{
95+
if (!tab || !tab.tab_id) return console.log('[', kleur.green("toolbar") , '] cannot close', tab && tab.tab_id);
96+
console.log('[', kleur.green("toolbar") , '] closing', tab.tab_id);
97+
icpChannel.sendSignalToRender('close-tab', Toolbar.toolbar_tab, tab.tab_id.substring(4));
7898
}
7999

80100
// force_url can be set to true ONLY from this extension's context, NOT from the browser's new tab request
@@ -85,11 +105,10 @@ class Toolbar extends require('../../lib/BaseModule')
85105
preload: path.join(__dirname, '../preload.js'), // Secure bridge
86106
...Env.WEBVIEW_DEFAULT_PREFERENCES
87107
}});
88-
TabsManager.setNewTab(newTab, "tab_" + this.tabs_count++);
89108
const tab_url = force_url ? requested_url : url.format(this.__conf.default_url);
90109
newTab.webContents.loadURL(tab_url);
91110
this.log("Created new tab at url:", tab_url);
92-
return getPageTitle(newTab.webContents);
111+
return {success: await TabsManager.newTab(newTab, "tab_" + this.tabs_count++), title: await getPageTitle(newTab.webContents)};
93112
}
94113

95114
setActiveTab(index) {
@@ -98,9 +117,10 @@ class Toolbar extends require('../../lib/BaseModule')
98117
// mainWindow.webContents.send("tab-switched", { index });
99118
}
100119

101-
closeTab(tabId)
120+
closeTab(tab_name)
102121
{
103-
TabsManager.closeTab(tabId);
122+
this.log('renderer requested tab close')
123+
TabsManager.closeTabName(tab_name);
104124
}
105125
}
106126

extensions/toolbar/new_page.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
5+
<h1>The a target attribute</h1>
6+
7+
<p>Open this very illegal webseit!: <a href="https://www.w3schools.com" target="_blank">Visit W3Schools!</a></p>
8+
<p>Or just the usual page example: <a href="https://www.google.com" target="_blank">Example!</a></p>
9+
10+
</body>
11+
</html>

extensions/tab_browser/toolbar.html renamed to extensions/toolbar/toolbar.html

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<script>
6363

6464
window.manage_tabs.onDoCreateTab(createTab);
65+
window.manage_tabs.onDoCloseTab(closeTabId);
6566

6667
const tabBar = document.getElementById('tabBar');
6768
const viewContainer = document.getElementById('viewContainer');
@@ -71,19 +72,26 @@
7172
let activeTab = null;
7273
let tabCount = 0;
7374

74-
newTabBtn.addEventListener('click', async () => {
75+
newTabBtn.addEventListener('click', request_tab, {once: true});
76+
77+
function request_tab()
78+
{
7579
console.log("new tab requested");
76-
requestNewTab('asdasdasdasd');
77-
});
80+
newTab('asdasdasdasd');
81+
}
7882

79-
async function requestNewTab(title)
83+
async function newTab(content)
8084
{
8185
activeTab = null;
82-
createTab(await window.manage_tabs.createdTab(title));
86+
createTab(await window.manage_tabs.createdTab(content));
87+
88+
newTabBtn.removeEventListener('click', request_tab);
89+
newTabBtn.addEventListener('click', request_tab, {once: true});
8390
}
8491

85-
async function createTab(title) {
92+
async function createTab({title, success}) {
8693
// create tab element
94+
if (success != true) return console.log('page creation not succesful');
8795
const tabEl = document.createElement('div');
8896
tabEl.classList.add('tab');
8997
tabEl.id = tabCount;
@@ -98,7 +106,6 @@
98106

99107
// event handlers
100108
tabEl.addEventListener("click", () => {
101-
window.manage_tabs.switchTab(parseInt(tabEl.dataset.index));
102109
activateTab(tabEl);
103110
});
104111
closeBtn.addEventListener('click', (e) => {
@@ -111,21 +118,38 @@
111118
}
112119

113120
function activateTab(tabEl) {
121+
console.log('switching to', parseInt(tabEl.dataset.index));
122+
window.manage_tabs.switchTab(parseInt(tabEl.dataset.index));
114123
tabs.forEach(t => t.classList.remove("active"));
115124
tabEl.classList.add("active");
116125
activeTab = tabEl;
117126
}
118127

119-
function closeTab(tabEl) {
120-
tabs.pop(tabEl.dataset.index);
128+
function closeTabId(tabId)
129+
{
130+
console.log("requested to close tab", tabId);
131+
tabs.forEach(tab => {
132+
if (tab.dataset.index === tabId)
133+
removeTab(tab);
134+
});
135+
}
136+
137+
function removeTab(tabEl)
138+
{
139+
console.log("ALL TABS:", tabs);
140+
tabs = tabs.filter(tab => tab.dataset.index !== tabEl.dataset.index);
141+
console.log("ALL TABS:", tabs);
142+
tabEl.remove();
143+
window.manage_tabs.closeTab(tabEl.dataset.index);
121144
if (tabs.length != 0)
122-
activateTab(tabs[0]);
123-
else
124-
requestNewTab('asdasdasdasd');
125-
console.log("closing tab");
126-
tabEl.remove();
127-
window.manage_tabs.closeTab(tabEl.dataset.index);
128-
delete tabEl;
145+
activateTab(tabs[0]);
146+
else
147+
newTab({title: 'asdasdasd', success: 'true'});
148+
}
149+
150+
function closeTab(tabEl) {
151+
console.log("closing tab", tabEl.dataset.index);
152+
removeTab(tabEl);
129153
}
130154
</script>
131155
</body>

extensions/tab_browser/toolbar_preload.js renamed to extensions/toolbar/toolbar_preload.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const { ipcRenderer } = require('electron');
2+
const BasePreload = require('../../lib/BasePreload.js');
23

3-
class Toolbar_preload extends require('../../lib/BasePreload.js')
4+
class Toolbar_preload extends BasePreload
45
{
56
MODULE_NAME = "toolbar";
67
contextbridge_expose = {
78
'manage_tabs': {
89
createdTab: (url) => ipcRenderer.invoke("created-tab", url),
910
switchTab: (index) => ipcRenderer.invoke("switch-tab", index),
10-
closeTab: (index) => ipcRenderer.invoke("close-tab", index),
11-
onDoCreateTab: (cb) => ipcRenderer.on("create-tab", (_, data) => cb(data))
11+
closeTab: (index) => ipcRenderer.invoke("closed-tab", index),
12+
onDoCreateTab: (cb) => ipcRenderer.on("create-tab", (_, data) => cb(data)),
13+
onDoCloseTab: (cb) => ipcRenderer.on("close-tab", (_, data) => cb(data))
1214
}
1315
}
1416

extensions/touch-utils/main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class DoubleClick extends require('../../lib/BaseModule')
2020

2121
setup() {}
2222

23-
onNewTabCreated()
23+
onNewTabCreated(newTab)
2424
{
25-
this.tab.webContents.on('input-event', (event, input) => {
25+
newTab.webContents.on('input-event', (event, input) => {
2626
if (input.type != 'mouseMove' && Env.VERBOSE)
2727
this.log("sending an", input.type);
2828
switch(input.type)
@@ -67,12 +67,12 @@ class DoubleClick extends require('../../lib/BaseModule')
6767
if (this.__conf.disable_physical_keyboard == true && this.__conf.disable_virtual_keyboard == true)
6868
{
6969
this.warn("disabling keyboard");
70-
this.tab.webContents.on("before-input-event", (event, input) => {if (input.type === 'keyDown' || input.type === 'keyUp') event.preventDefault();});
70+
newTab.webContents.on("before-input-event", (event, input) => {if (input.type === 'keyDown' || input.type === 'keyUp') event.preventDefault();});
7171
}
7272
else if (this.__conf.disable_physical_keyboard == true)
7373
{
7474
this.warn("disabling physical keyboard");
75-
this.tab.webContents.on("before-input-event", (event, input) => {
75+
newTab.webContents.on("before-input-event", (event, input) => {
7676
if (!this.mouseHasLeft && input.type === 'keyDown' || input.type === 'keyUp')
7777
{
7878
event.preventDefault();
@@ -83,7 +83,7 @@ class DoubleClick extends require('../../lib/BaseModule')
8383
else if (this.__conf.disable_virtual_keyboard == true)
8484
{
8585
this.warn("disabling virtual keyboard");
86-
this.tab.webContents.on("before-input-event", (event, input) => {
86+
newTab.webContents.on("before-input-event", (event, input) => {
8787
if (this.mouseHasLeft && input.type === 'keyDown' || input.type === 'keyUp')
8888
{
8989
event.preventDefault();

extensions/webpage-filter/main.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const url = require('url');
2+
const Toolbar = require('../toolbar/main');
3+
const BaseModule = require('../../lib/BaseModule');
4+
5+
class WebpageFilter extends BaseModule
6+
{
7+
MODULE_NAME = 'webpage-filter';
8+
9+
setup()
10+
{
11+
if (this.__conf.hosts_blacklist)
12+
this.log('Host blacklist:', this.__conf.hosts_blacklist);
13+
if (this.__conf.hosts_whitelist)
14+
this.log('Host whitelist:', this.__conf.hosts_whitelist);
15+
}
16+
17+
onNewTabCreated(newTab)
18+
{
19+
newTab.webContents.once('did-start-navigation', (event, req_url) => {
20+
this.warn('Checking url:', req_url);
21+
if (this.isBadPage(url.parse(req_url)))
22+
Toolbar.requestCloseTab(newTab);
23+
});
24+
}
25+
26+
// false -> page OK. true -> page NOT OK.
27+
isBadPage(page_info)
28+
{
29+
this.warn('page info:', page_info);
30+
let host = page_info.host;
31+
const host_split = host.split('.');
32+
if (host_split.length > 2)
33+
host = host_split.slice(1, host_split.length - 1).join("");
34+
this.warn('detected host:', host);
35+
if (host == 'blank')
36+
return false;
37+
if (this.__conf.hosts_blacklist && this.__conf.hosts_blacklist.includes(host))
38+
return true;
39+
if (this.__conf.hosts_whitelist && this.__conf.hosts_whitelist.length != 0)
40+
return this.__conf.hosts_whitelist.includes(host) ? false : true;
41+
return false;
42+
}
43+
}
44+
45+
module.exports = WebpageFilter;

0 commit comments

Comments
 (0)