@@ -37,19 +37,12 @@ let htmlTemplate = '<!DOCTYPE html><html><head><meta charset="UTF-8"><style type
37
37
let fileExt ;
38
38
let currentTabURL ;
39
39
let currentTabID ;
40
+ let htmlOriginal ;
41
+ let htmlCleaned ;
42
+ let htmlSelection ;
43
+ let contentMode = 'simplified' ; // 'original'
40
44
const activeTabQuery = browser . tabs . query ( { currentWindow : true , active : true } ) ;
41
45
42
- // Geo locations:
43
- // GMaps: https://www.google.de/maps/@48.1401285,11.5732137,15.25z
44
- // GMaps: https://www.google.de/maps/@-20.8096591,-49.3801033,16z
45
- // OpenStreetMap: https://www.openstreetmap.org/#map=17/48.13504/11.59057
46
- // OpenStreetMap: https://www.openstreetmap.org/#map=16/-20.8077/-49.3785
47
- // Here: https://wego.here.com/?map=-20.80625,-49.37421,16,normal
48
- // Bing: no url param
49
- // https://regexper.com
50
- // RegEx: ^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)[,/][-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
51
- // RegEx: ^(\-?\d+(\.\d+)?)[,/](\-?\d+(\.\d+)?)$
52
-
53
46
function init ( ) {
54
47
// console.log('Settings: ' + JSON.stringify(userSettings));
55
48
@@ -58,6 +51,7 @@ function init() {
58
51
currentTabURL = tab . url ;
59
52
currentTabID = tab . id ;
60
53
currentTabURLParser . href = currentTabURL ;
54
+ extractLatLong ( ) ;
61
55
fileExt = extractFileExtFromUrl ( ) ;
62
56
$ ( '#title' ) . val ( tab . title . substring ( tab . title . lastIndexOf ( "/" ) + 1 , tab . title . length ) ) ;
63
57
@@ -74,7 +68,8 @@ function init() {
74
68
$ ( "#saveWholePageAsHtml" ) . on ( "click" , saveWholePageAsHTML ) ;
75
69
$ ( "#saveScreenshot" ) . on ( "click" , saveScreenshot ) ;
76
70
$ ( "#downloadFile" ) . on ( 'click' , downloadFile ) ;
77
-
71
+ $ ( "#simplifiedPreview" ) . on ( 'click' , simplifiedPreview ) ;
72
+ $ ( "#fullPreview" ) . on ( 'click' , fullPreview ) ;
78
73
79
74
// I18n this panel
80
75
$ ( '[data-i18n]' ) . each ( function ( ) {
@@ -84,7 +79,57 @@ function init() {
84
79
$ ( this ) . attr ( "title" , browser . i18n . getMessage ( $ ( this ) . data ( 'i18n-title' ) ) ) ;
85
80
} ) ;
86
81
87
- browser . runtime . onMessage . addListener ( handleHTMLContent ) ;
82
+ browser . runtime . onMessage . addListener ( handleHTML ) ;
83
+
84
+ browser . tabs . executeScript ( null , {
85
+ file : "content-script-capture-wholepage.dist.js"
86
+ } ) . then ( ( ) => {
87
+ console . log ( 'Content script injected...' ) ;
88
+ } , ( err ) => {
89
+ console . warn ( 'Error executing script ' + JSON . stringify ( err ) ) ;
90
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( 'Error while capturing content' ) ;
91
+ // alert('Error getting content from the current tab.')
92
+ // location.reload();
93
+ } ) ;
94
+
95
+ browser . tabs . executeScript ( null , {
96
+ file : "content-script-capture-selection.dist.js"
97
+ } ) . then ( ( ) => {
98
+ console . log ( 'Content script injected...' ) ;
99
+ } , ( err ) => {
100
+ console . warn ( 'Error executing script ' + JSON . stringify ( err ) ) ;
101
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( 'Error while capturing content' ) ;
102
+ // alert('Error getting content from the current tab.')
103
+ // location.reload();
104
+ } ) ;
105
+ }
106
+
107
+ // Geo locations:
108
+ // GMaps: https://www.google.de/maps/@48.1401285,11.5732137,15.25z
109
+ // GMaps: https://www.google.de/maps/@-20.8096591,-49.3801033,16z
110
+ // OpenStreetMap: https://www.openstreetmap.org/#map=17/48.13504/11.59057
111
+ // OpenStreetMap: https://www.openstreetmap.org/#map=16/-20.8077/-49.3785
112
+ // Here: https://wego.here.com/?map=-20.80625,-49.37421,16,normal
113
+ // Bing: no url param
114
+ function extractLatLong ( ) {
115
+ const regex = new RegExp ( '@(.*),(.*),' ) ;
116
+ // const regex = /@(.*),(.*),/gm;
117
+ // https://regexper.com
118
+ // RegEx: ^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)[,/][-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
119
+ // RegEx: ^(\-?\d+(\.\d+)?)[,/](\-?\d+(\.\d+)?)$
120
+ // alert(currentTabURLParser.href);
121
+ if ( currentTabURLParser . href ) {
122
+ const lonLatMatch = currentTabURLParser . href . match ( regex ) ;
123
+ if ( lonLatMatch && lonLatMatch . length > 1 ) {
124
+ let lon = lonLatMatch [ 1 ] ;
125
+ let lat = lonLatMatch [ 2 ] ;
126
+ if ( ! lat . startsWith ( '-' ) ) {
127
+ lat = '+' + lat ;
128
+ }
129
+ const geoTag = lon + lat ;
130
+ $ ( '#tags' ) . val ( $ ( '#tags' ) . val ( ) + ' ' + geoTag ) ;
131
+ }
132
+ }
88
133
}
89
134
90
135
function saveAsFile ( blob , filename ) {
@@ -99,25 +144,51 @@ function saveAsFile(blob, filename) {
99
144
}
100
145
}
101
146
102
- function handleHTMLContent ( request ) {
147
+ function handleHTML ( request ) {
103
148
if ( request . action == "htmlcontent" ) {
149
+ // console.log("HTML: " + request.source);
150
+ htmlOriginal = request . originalHTML ;
151
+ htmlCleaned = request . cleanedHTML ;
152
+ }
153
+ if ( request . action == "htmlselection" ) {
104
154
// console.log("HTML: " + request.source);
105
155
if ( request . source . length < 1 ) {
106
- alert ( 'No content selected....' ) ;
107
- return ;
156
+ // alert('No content selected....');
157
+ } else {
158
+ htmlSelection = request . source ;
108
159
}
109
- prepareContentPromise ( request . source ) . then ( ( cleanenHTML ) => {
110
- const htmlBlob = new Blob ( [ cleanenHTML ] , {
111
- type : "text/html;charset=utf-8"
112
- } ) ;
113
- saveAsFile ( htmlBlob , generateFileName ( 'html' ) ) ;
114
- $ ( '#saveWholePageAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file' ) ;
115
- $ ( '#saveSelectionAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file-text' ) ;
116
- } ) . catch ( ( err ) => {
117
- alert ( 'Error by preparing the HTML content...' ) ;
118
- location . reload ( ) ;
119
- console . warn ( 'Error handling html content ' + err ) ;
120
- } ) ;
160
+ }
161
+ if ( htmlSelection ) {
162
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( htmlSelection ) ;
163
+ $ ( '#saveWholePageAsHtml' ) . attr ( "disabled" , true ) ;
164
+ return ;
165
+ } else if ( htmlCleaned ) {
166
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( htmlCleaned ) ;
167
+ $ ( '#saveSelectionAsHtml' ) . attr ( "disabled" , true ) ;
168
+ contentMode = 'simplified' ;
169
+ return ;
170
+ } else if ( htmlOriginal ) {
171
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( htmlOriginal ) ;
172
+ $ ( '#saveSelectionAsHtml' ) . attr ( "disabled" , true ) ;
173
+ contentMode = 'original' ;
174
+ return ;
175
+ } else {
176
+ $ ( '#contentModeSwitch' ) . hide ( ) ;
177
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( 'No content was extracted...' ) ;
178
+ }
179
+ }
180
+
181
+ function simplifiedPreview ( ) {
182
+ if ( htmlCleaned ) {
183
+ contentMode = 'simplified' ;
184
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( htmlCleaned ) ;
185
+ }
186
+ }
187
+
188
+ function fullPreview ( ) {
189
+ if ( htmlOriginal ) {
190
+ contentMode = 'original' ;
191
+ $ ( '#preview' ) . contents ( ) . find ( 'html' ) . html ( htmlOriginal ) ;
121
192
}
122
193
}
123
194
@@ -141,29 +212,47 @@ function downloadFile() {
141
212
142
213
function saveWholePageAsHTML ( ) {
143
214
$ ( '#saveWholePageAsHtml i' ) . removeClass ( 'fa-file' ) . addClass ( 'fa-spin fa-circle-o-notch' ) ;
144
- const executing = browser . tabs . executeScript ( null , {
145
- file : "content-script-capture-wholepage.dist.js"
146
- } ) ;
147
- executing . then ( ( ) => {
148
- console . log ( 'Content script injected...' ) ;
149
- } , ( err ) => {
150
- console . warn ( 'Error executing script ' + JSON . stringify ( err ) ) ;
151
- alert ( 'Error getting content from the current tab.' )
215
+ let content = '' ;
216
+ if ( contentMode === 'simplified' ) {
217
+ content = htmlCleaned ;
218
+ } else if ( contentMode === 'original' ) {
219
+ content = htmlOriginal ;
220
+ }
221
+ if ( content < 1 ) {
222
+ alert ( 'No content extracted....' ) ;
223
+ return ;
224
+ }
225
+ prepareContentPromise ( content ) . then ( ( convertedHTML ) => {
226
+ const htmlBlob = new Blob ( [ convertedHTML ] , {
227
+ type : "text/html;charset=utf-8"
228
+ } ) ;
229
+ saveAsFile ( htmlBlob , generateFileName ( 'html' ) ) ;
230
+ $ ( '#saveWholePageAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file' ) ;
231
+ $ ( '#saveSelectionAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file-text' ) ;
232
+ } ) . catch ( ( err ) => {
233
+ alert ( 'Error by preparing the HTML content...' ) ;
152
234
location . reload ( ) ;
235
+ console . warn ( 'Error handling html content ' + err ) ;
153
236
} ) ;
154
237
}
155
238
156
239
function saveSelectionAsHTML ( ) {
157
240
$ ( '#saveSelectionAsHtml i' ) . removeClass ( 'fa-file-text' ) . addClass ( 'fa-spin fa-circle-o-notch' ) ;
158
- const executing = browser . tabs . executeScript ( null , {
159
- file : "content-script-capture-selection.dist.js"
160
- } ) ;
161
- executing . then ( ( ) => {
162
- console . log ( 'Content script injected...' ) ;
163
- } , ( err ) => {
164
- console . warn ( 'Error executing script ' + JSON . stringify ( err ) ) ;
165
- alert ( 'Error getting content from the current tab.' )
166
- location . reload ( ) ;
241
+ if ( htmlSelection < 1 ) {
242
+ alert ( 'No content selected....' ) ;
243
+ return ;
244
+ }
245
+ prepareContentPromise ( htmlSelection ) . then ( ( cleanenHTML ) => {
246
+ const htmlBlob = new Blob ( [ cleanenHTML ] , {
247
+ type : "text/html;charset=utf-8"
248
+ } ) ;
249
+ saveAsFile ( htmlBlob , generateFileName ( 'html' ) ) ;
250
+ $ ( '#saveWholePageAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file' ) ;
251
+ $ ( '#saveSelectionAsHtml i' ) . removeClass ( 'fa-spin fa-circle-o-notch' ) . addClass ( 'fa-file-text' ) ;
252
+ } ) . catch ( ( err ) => {
253
+ alert ( 'Error by preparing the HTML content...' ) ;
254
+ // location.reload();
255
+ console . warn ( 'Error handling html content ' + err ) ;
167
256
} ) ;
168
257
}
169
258
0 commit comments