1p-filters.js (12219B) - View raw
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385/******************************************************************************* uBlock Origin - a comprehensive, efficient content blocker Copyright (C) 2014-present Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. Home: https://github.com/gorhill/uBlock */ /* global CodeMirror, uBlockDashboard */ import './codemirror/ubo-static-filtering.js'; import { dom, qs$ } from './dom.js'; import { i18n$ } from './i18n.js'; import { onBroadcast } from './broadcast.js'; /******************************************************************************/ const cmEditor = new CodeMirror(qs$('#userFilters'), { autoCloseBrackets: true, autofocus: true, extraKeys: { 'Ctrl-Space': 'autocomplete', 'Tab': 'toggleComment', }, foldGutter: true, gutters: [ 'CodeMirror-linenumbers', { className: 'CodeMirror-lintgutter', style: 'width: 11px' }, ], lineNumbers: true, lineWrapping: true, matchBrackets: true, maxScanLines: 1, styleActiveLine: { nonEmpty: true, }, }); uBlockDashboard.patchCodeMirrorEditor(cmEditor); /******************************************************************************/ // Add auto-complete ability to the editor. Polling is used as the suggested // hints also depend on the tabs currently opened. { let hintUpdateToken = 0; const getHints = async function() { const hints = await vAPI.messaging.send('dashboard', { what: 'getAutoCompleteDetails', hintUpdateToken }); if ( hints instanceof Object === false ) { return; } if ( hints.hintUpdateToken !== undefined ) { cmEditor.setOption('uboHints', hints); hintUpdateToken = hints.hintUpdateToken; } timer.on(2503); }; const timer = vAPI.defer.create(( ) => { getHints(); }); getHints(); } vAPI.messaging.send('dashboard', { what: 'getTrustedScriptletTokens', }).then(tokens => { cmEditor.setOption('trustedScriptletTokens', tokens); }); /******************************************************************************/ let originalState = { enabled: true, trusted: false, filters: '', }; function getCurrentState() { const enabled = qs$('#enableMyFilters input').checked; return { enabled, trusted: qs$('#trustMyFilters input').checked, filters: getEditorText(), }; } function rememberCurrentState() { originalState = getCurrentState(); } function currentStateChanged() { return JSON.stringify(getCurrentState()) !== JSON.stringify(originalState); } function getEditorText() { const text = cmEditor.getValue().trimEnd(); return text === '' ? text : `${text}\n`; } function setEditorText(text) { cmEditor.setValue(`${text.trimEnd()}\n\n`); } /******************************************************************************/ function userFiltersChanged(details = {}) { const changed = typeof details.changed === 'boolean' ? details.changed : self.hasUnsavedData(); qs$('#userFiltersApply').disabled = !changed; qs$('#userFiltersRevert').disabled = !changed; const enabled = qs$('#enableMyFilters input').checked; const trustedbefore = cmEditor.getOption('trustedSource'); const trustedAfter = enabled && qs$('#trustMyFilters input').checked; if ( trustedAfter === trustedbefore ) { return; } cmEditor.startOperation(); cmEditor.setOption('trustedSource', trustedAfter); const doc = cmEditor.getDoc(); const history = doc.getHistory(); const selections = doc.listSelections(); doc.replaceRange(doc.getValue(), { line: 0, ch: 0 }, { line: doc.lineCount(), ch: 0 } ); doc.setSelections(selections); doc.setHistory(history); cmEditor.endOperation(); cmEditor.focus(); } /******************************************************************************/ // https://github.com/gorhill/uBlock/issues/3704 // Merge changes to user filters occurring in the background with changes // made in the editor. The code assumes that no deletion occurred in the // background. function threeWayMerge(newContent) { const prvContent = originalState.filters.trim().split(/\n/); const differ = new self.diff_match_patch(); const newChanges = differ.diff( prvContent, newContent.trim().split(/\n/) ); const usrChanges = differ.diff( prvContent, getEditorText().trim().split(/\n/) ); const out = []; let i = 0, j = 0, k = 0; while ( i < prvContent.length ) { for ( ; j < newChanges.length; j++ ) { const change = newChanges[j]; if ( change[0] !== 1 ) { break; } out.push(change[1]); } for ( ; k < usrChanges.length; k++ ) { const change = usrChanges[k]; if ( change[0] !== 1 ) { break; } out.push(change[1]); } if ( k === usrChanges.length || usrChanges[k][0] !== -1 ) { out.push(prvContent[i]); } i += 1; j += 1; k += 1; } for ( ; j < newChanges.length; j++ ) { const change = newChanges[j]; if ( change[0] !== 1 ) { continue; } out.push(change[1]); } for ( ; k < usrChanges.length; k++ ) { const change = usrChanges[k]; if ( change[0] !== 1 ) { continue; } out.push(change[1]); } return out.join('\n'); } /******************************************************************************/ async function renderUserFilters(merge = false) { const details = await vAPI.messaging.send('dashboard', { what: 'readUserFilters', }); if ( details instanceof Object === false || details.error ) { return; } cmEditor.setOption('trustedSource', details.trusted); qs$('#enableMyFilters input').checked = details.enabled; qs$('#trustMyFilters input').checked = details.trusted; const newContent = details.content.trim(); if ( merge && self.hasUnsavedData() ) { setEditorText(threeWayMerge(newContent)); userFiltersChanged({ changed: true }); } else { setEditorText(newContent); userFiltersChanged({ changed: false }); } rememberCurrentState(); } /******************************************************************************/ function handleImportFilePicker(ev) { const file = ev.target.files[0]; if ( file === undefined || file.name === '' ) { return; } if ( file.type.indexOf('text') !== 0 ) { return; } const fr = new FileReader(); fr.onload = function() { if ( typeof fr.result !== 'string' ) { return; } const content = uBlockDashboard.mergeNewLines(getEditorText(), fr.result); cmEditor.operation(( ) => { const cmPos = cmEditor.getCursor(); setEditorText(content); cmEditor.setCursor(cmPos); cmEditor.focus(); }); }; fr.readAsText(file); } dom.on('#importFilePicker', 'change', handleImportFilePicker); function startImportFilePicker() { const input = qs$('#importFilePicker'); // Reset to empty string, this will ensure an change event is properly // triggered if the user pick a file, even if it is the same as the last // one picked. input.value = ''; input.click(); } dom.on('#importUserFiltersFromFile', 'click', startImportFilePicker); /******************************************************************************/ function exportUserFiltersToFile() { const val = getEditorText(); if ( val === '' ) { return; } const filename = i18n$('1pExportFilename') .replace('{{datetime}}', uBlockDashboard.dateNowToSensibleString()) .replace(/ +/g, '_'); vAPI.download({ 'url': `data:text/plain;charset=utf-8,${encodeURIComponent(val)}`, 'filename': filename }); } /******************************************************************************/ async function applyChanges() { const state = getCurrentState(); const details = await vAPI.messaging.send('dashboard', { what: 'writeUserFilters', content: state.filters, enabled: state.enabled, trusted: state.trusted, }); if ( details instanceof Object === false || details.error ) { return; } rememberCurrentState(); userFiltersChanged({ changed: false }); vAPI.messaging.send('dashboard', { what: 'reloadAllFilters', }); } function revertChanges() { qs$('#enableMyFilters input').checked = originalState.enabled; qs$('#trustMyFilters input').checked = originalState.trusted; setEditorText(originalState.filters); userFiltersChanged(); } /******************************************************************************/ function getCloudData() { return getEditorText(); } function setCloudData(data, append) { if ( typeof data !== 'string' ) { return; } if ( append ) { data = uBlockDashboard.mergeNewLines(getEditorText(), data); } cmEditor.setValue(data); } self.cloud.onPush = getCloudData; self.cloud.onPull = setCloudData; /******************************************************************************/ self.wikilink = 'https://github.com/gorhill/uBlock/wiki/Dashboard:-My-filters'; self.hasUnsavedData = function() { return currentStateChanged(); }; /******************************************************************************/ // Handle user interaction dom.on('#exportUserFiltersToFile', 'click', exportUserFiltersToFile); dom.on('#userFiltersApply', 'click', ( ) => { applyChanges(); }); dom.on('#userFiltersRevert', 'click', revertChanges); dom.on('#enableMyFilters input', 'change', userFiltersChanged); dom.on('#trustMyFilters input', 'change', userFiltersChanged); (async ( ) => { await renderUserFilters(); cmEditor.clearHistory(); // https://github.com/gorhill/uBlock/issues/3706 // Save/restore cursor position { const line = await vAPI.localStorage.getItemAsync('myFiltersCursorPosition'); if ( typeof line === 'number' ) { cmEditor.setCursor(line, 0); } cmEditor.focus(); } // https://github.com/gorhill/uBlock/issues/3706 // Save/restore cursor position { let curline = 0; cmEditor.on('cursorActivity', ( ) => { if ( timer.ongoing() ) { return; } if ( cmEditor.getCursor().line === curline ) { return; } timer.on(701); }); const timer = vAPI.defer.create(( ) => { curline = cmEditor.getCursor().line; vAPI.localStorage.setItem('myFiltersCursorPosition', curline); }); } // https://github.com/gorhill/uBlock/issues/3704 // Merge changes to user filters occurring in the background onBroadcast(msg => { switch ( msg.what ) { case 'userFiltersUpdated': { cmEditor.startOperation(); const scroll = cmEditor.getScrollInfo(); const selections = cmEditor.listSelections(); renderUserFilters(true).then(( ) => { cmEditor.clearHistory(); cmEditor.setSelection(selections[0].anchor, selections[0].head); cmEditor.scrollTo(scroll.left, scroll.top); cmEditor.endOperation(); }); break; } default: break; } }); })(); cmEditor.on('changes', userFiltersChanged); CodeMirror.commands.save = applyChanges; /******************************************************************************/