-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwindow.js
161 lines (149 loc) · 5.49 KB
/
window.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*global chrome, analytics */
const webview = document.getElementById('panel-container'),
window_title = document.getElementById('document-title'),
favicon_image = document.getElementById('document-favicon'),
appID = chrome.i18n.getMessage('@@extension_id'), // this app
NODE_TLS_REJECT_UNAUTHORIZED = '0';// allow self-signed certificates
var webview_zoom_level = null;
// Set locale texts
document.querySelectorAll('.locale').forEach(function(locale){ locale.innerText = chrome.i18n.getMessage(locale.id); });
// Exception for element who can't use .locale
// Get URL
window.addEventListener('load', function(e) {
chrome.storage.sync.get(function(items) {
if (items.url !== undefined && items.url !== ''){
window.tracker.sendEvent('Browser', 'Load URL', items.url);
webview.setAttribute('src', items.url);
webview.getZoom(function(zoomFactor){webview_zoom_level = zoomFactor;});
} else{
var SwitchBecauseNoUrl = analytics.EventBuilder.builder()
.category('App')
.action('Switch')
.dimension(2, 'Missing URL');
window.tracker.send(SwitchBecauseNoUrl).addCallback(function() {
chrome.runtime.sendMessage({'open': 'options'});
}.bind(this));
}
});
});
// inset styles and scripts
favicon_image.addEventListener('loadcommit', function(e) {
if (e.isTopLevel) {
favicon_image.insertCSS({
file: './styles/favicon_webview.css',
runAt: 'document_start'
});
}
});
webview.addEventListener('loadcommit', function(e) {
if (e.isTopLevel) {
webview.insertCSS({
file: './styles/inner_webview.css',
runAt: 'document_start'
});
// Set app title to document title
var titleFirstTime = true;
webview.executeScript(
{
code: 'document.title',
runAt: 'document_end'
},
function(results){
if (results && results[0]) {
document.title = results[0];
window_title.innerText = results[0];
} else {
if (!titleFirstTime) {
var AlertNoTitle = analytics.EventBuilder.builder()
.category('Errors')
.action('Alert')
.dimension(3, 'No document title');
window.tracker.send(AlertNoTitle);
} else
titleFirstTime = false;
}
}
);
webview.executeScript(
{
code: 'document.location.hostname',
runAt: 'document_end'
},
function(results){
if (results && results[0]) {
fetch("https://favicongrabber.com/api/grab/" + results[0])
.then(response => response.json())
.then(({ icons }) => {
if (icons[0]?.src)
favicon_image.src = icons[0]?.src
})
}
}
);
}
});
// send new-window-links to browser
webview.addEventListener('newwindow', function(e) {
e.stopImmediatePropagation();
window.tracker.sendEvent('Browser', 'New window', e.targetUrl).addCallback(function() {
window.open(e.targetUrl);
}.bind(this));
});
// fix webview lose focus
window.addEventListener('focus', function(e) {
webview.focus();
});
// allow download and fullscreen
webview.addEventListener('permissionrequest', function(e) {
if (e.permission === 'download' || e.permission === 'fullscreen') {
e.request.allow();
}
});
// open cached links
chrome.runtime.onMessage.addListener(function(request, sender) {
if (sender.id == appID) {
webview.src = request;
}
if (typeof request.dontresize !== 'undefined') {
if (request.dontresize) webview.classList.remove('resize');
else webview.classList.add('resize');
}
});
// Learn and improve from app usage
window.addEventListener('load', function() {
// Initialize the Analytics service object with the name of your app.
const service = analytics.getService('cornips_fbw');
// Get a Tracker using your Google Analytics app Tracking ID.
window.tracker = service.getTracker('UA-84858849-3');
// Record an "appView" each time the user launches the app
window.tracker.sendAppView('WindowView');
// Track locale
var locale = chrome.i18n.getUILanguage();
var InitLanguage;
switch(locale) {
case 'pl':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('WindowView')
.dimension(4, 'Polish');
break;
case 'de':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('WindowView')
.dimension(3, 'German');
break;
case 'nl':
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('WindowView')
.dimension(2, 'Dutch');
break;
default:
InitLanguage = analytics.EventBuilder.builder()
.category('Language')
.action('WindowView')
.dimension(1, 'English');
}
window.tracker.send(InitLanguage);
});