diff --git a/chat/preview/ImagePreview.vue b/chat/preview/ImagePreview.vue index 332639d..8a192b5 100644 --- a/chat/preview/ImagePreview.vue +++ b/chat/preview/ImagePreview.vue @@ -44,10 +44,6 @@ import Timer = NodeJS.Timer; import IpcMessageEvent = Electron.IpcMessageEvent; - import { ElectronBlocker } from '@cliqz/adblocker-electron'; - import fetch from 'node-fetch'; - - // import { promises as fs } from 'fs'; const screen = remote.screen; @@ -164,14 +160,14 @@ const e = event as DidFailLoadEvent; if (e.errorCode < 0) { - console.error('DID FAIL LOAD', event); - const url = this.getUrl() || ''; - - const qjs = this.jsMutator.getMutatorJsForSite(url, 'update-target-url') - || this.jsMutator.getMutatorJsForSite(url, 'dom-ready'); - - // tslint:disable-next-line - this.executeJavaScript(qjs, 'did-fail-load-but-still-loading', event); + // console.error('DID FAIL LOAD', event); + // const url = this.getUrl() || ''; + // + // const qjs = this.jsMutator.getMutatorJsForSite(url, 'update-target-url') + // || this.jsMutator.getMutatorJsForSite(url, 'dom-ready'); + // + // // tslint:disable-next-line + // this.executeJavaScript(qjs, 'did-fail-load-but-still-loading', event); return; } @@ -261,45 +257,9 @@ }, 50 ); - - // this.initAdBlocker(); } - async initAdBlocker() { - const webview = this.getWebview(); - const contents = remote.webContents.fromId(webview.getWebContentsId()); - - console.log('INITADBLOCKER'); - - const blocker = await ElectronBlocker.fromLists( - fetch, - [ - 'https://easylist.to/easylist/easylist.txt', - 'https://easylist.to/easylist/easyprivacy.txt', // EasyPrivacy - 'https://easylist.to/easylist/fanboy-social.txt', // Fanboy Social - 'https://easylist.to/easylist/fanboy-annoyance.txt', // Fanboy Annoyances - 'https://filters.adtidy.org/extension/chromium/filters/2.txt', // AdGuard Base - 'https://filters.adtidy.org/extension/chromium/filters/11.txt', // AdGuard Mobile Ads - 'https://filters.adtidy.org/extension/chromium/filters/4.txt', // AdGuard Social Media - 'https://filters.adtidy.org/extension/chromium/filters/14.txt', // AdGuard Annoyances - 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt', // uBlock Origin Filters - 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/privacy.txt', // uBlock Origin Privacy - 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/resource-abuse.txt', // uBlock Origin Resource Abuse - ], - { - enableCompression: true, - }, - // { - // path: 'engine.bin', - // read: fs.readFile, - // write: fs.writeFile - // } - ); - - blocker.enableBlockingInSession(contents.session); - } - reRenderStyles(): void { // tslint:disable-next-line:no-unsafe-any this.externalPreviewStyle = this.externalPreviewHelper.renderStyle(); diff --git a/chat/preview/helper/external.ts b/chat/preview/helper/external.ts index 51b8cd1..0a046ac 100644 --- a/chat/preview/helper/external.ts +++ b/chat/preview/helper/external.ts @@ -21,8 +21,10 @@ export class ExternalImagePreviewHelper extends ImagePreviewHelper { const webview = this.parent.getWebview(); if (this.allowCachedUrl) { + // tslint:disable-next-line:no-floating-promises webview.executeJavaScript(this.parent.jsMutator.getHideMutator()); } else { + // tslint:disable-next-line:no-floating-promises webview.loadURL('about:blank'); } @@ -55,30 +57,42 @@ export class ExternalImagePreviewHelper extends ImagePreviewHelper { } // const oldUrl = this.url; - const oldLastExternalUrl = this.lastExternalUrl; + // const oldLastExternalUrl = this.lastExternalUrl; this.url = url; this.lastExternalUrl = url; this.visible = true; try { - if ((this.allowCachedUrl) && ((webview.getURL() === url) || (url === oldLastExternalUrl))) { - if (this.debug) - console.log('ImagePreview: exec re-show mutator'); + // if ((this.allowCachedUrl) && ((webview.getURL() === url) || (url === oldLastExternalUrl))) { + // if (this.debug) + // console.log('ImagePreview: exec re-show mutator'); + // + // // tslint:disable-next-line:no-floating-promises + // webview.executeJavaScript(this.parent.jsMutator.getReShowMutator()); + // } else { + // if (this.debug) + // console.log('ImagePreview: must load; skip re-show because urls don\'t match', this.url, webview.getURL()); - webview.executeJavaScript(this.parent.jsMutator.getReShowMutator()); - } else { - if (this.debug) - console.log('ImagePreview: must load; skip re-show because urls don\'t match', this.url, webview.getURL()); + this.ratio = null; - this.ratio = null; + webview.stop(); - // Broken promise chain on purpose - // tslint:disable-next-line:no-floating-promises - this.urlMutator.resolve(url) - .then((finalUrl: string) => webview.loadURL(finalUrl)); - } + // Broken promise chain on purpose + // tslint:disable-next-line:no-floating-promises + this.urlMutator.resolve(url) + .then( + async(finalUrl: string) => { + if (this.debug) + console.log('ImagePreview: must load', finalUrl, this.url, webview.getURL()); + webview.stop(); + + return webview.loadURL(finalUrl); + } + ); + + // } } catch (err) { console.error('ImagePreview: Webview reuse error', err); } diff --git a/chat/preview/image-preview-mutator.ts b/chat/preview/image-preview-mutator.ts index ccd27e6..564a743 100644 --- a/chat/preview/image-preview-mutator.ts +++ b/chat/preview/image-preview-mutator.ts @@ -237,27 +237,24 @@ export class ImagePreviewMutator { ]; const resolveImgSize = function() { - return sizePairs.reduce( - (acc, val) => { - if ((acc.width) && (acc.height)) { - return acc; - } + const solved = {}; - if ((img[val[0]]) && (img[val[1]])) { - return { - width: img[val[0]], - height: img[val[1]] - } - } + for (let ri = 0; ri < sizePairs.length; ri++) { + const val = sizePairs[ri]; - return acc; - }, - {} - ); + if ((img[val[0]]) && (img[val[1]])) { + solved.width = img[val[0]]; + solved.height = img[val[1]]; + break; + } + } + + return solved; } - const imSize = resolveImgSize(); - ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height); + + const preImSize = resolveImgSize(); + ipcRenderer.sendToHost('webview.img', preImSize.width, preImSize.height, 'preImSize'); const el = document.createElement('div'); el.id = 'flistWrapper'; @@ -297,6 +294,9 @@ export class ImagePreviewMutator { + 'min-width: initial !important; min-height: initial !important; max-width: initial !important; max-height: initial !important;' + 'display: block !important; visibility: visible !important;'; + img.removeAttribute('width'); + img.removeAttribute('height'); + img.class = ''; el.class = ''; html.class = ''; @@ -331,7 +331,7 @@ export class ImagePreviewMutator { ${this.debug ? "console.log('on DOMContentLoaded');" : ''} const imSize = resolveImgSize(); - ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height); + ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height, 'dom-content-loaded'); if ( (img.play) @@ -344,7 +344,7 @@ export class ImagePreviewMutator { ${this.debug ? "console.log('on load');" : ''} const imSize = resolveImgSize(); - ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height); + ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height, 'load'); if ( (img.play) @@ -361,6 +361,19 @@ export class ImagePreviewMutator { } + const updateSize = () => { + const imSize = resolveImgSize(); + + if ((imSize.width) && (imSize.height)) { + ipcRenderer.sendToHost('webview.img', imSize.width, imSize.height, 'updateSize'); + } else { + setTimeout(() => updateSize(), 200); + } + } + + updateSize(); + + let removeList = []; const safeIds = ['flistWrapper', 'flistError', 'flistHider']; const safeTags = [${_.map(safeTags, (t) => `'${t.toLowerCase()}'`).join(',')}]; diff --git a/electron/Window.vue b/electron/Window.vue index 6501047..0dc1adb 100644 --- a/electron/Window.vue +++ b/electron/Window.vue @@ -85,7 +85,7 @@ @Hook('mounted') async mounted(): Promise { // top bar devtools - browserWindow.webContents.openDevTools( { mode: 'detach' } ); + // browserWindow.webContents.openDevTools({ mode: 'detach' }); await this.addTab(); diff --git a/electron/chat.ts b/electron/chat.ts index 6ad9688..6e1a65a 100644 --- a/electron/chat.ts +++ b/electron/chat.ts @@ -186,7 +186,8 @@ let dictDir = path.join(electron.remote.app.getPath('userData'), 'spellchecker') if(process.platform === 'win32') //get the path in DOS (8-character) format as special characters cause problems otherwise exec(`for /d %I in ("${dictDir}") do @echo %~sI`, (_, stdout) => dictDir = stdout.trim()); -// electron.webFrame.setSpellCheckProvider('', {spellCheck: (words, callback) => callback(words.filter((x) => spellchecker.isMisspelled(x)))}); +// electron.webFrame.setSpellCheckProvider( +// '', {spellCheck: (words, callback) => callback(words.filter((x) => spellchecker.isMisspelled(x)))}); function onSettings(s: GeneralSettings): void { settings = s; diff --git a/electron/main.ts b/electron/main.ts index ee85616..76fe371 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -47,6 +47,7 @@ import BrowserWindow = Electron.BrowserWindow; import MenuItem = Electron.MenuItem; import { ElectronBlocker } from '@cliqz/adblocker-electron'; import fetch from 'node-fetch'; +import MenuItemConstructorOptions = Electron.MenuItemConstructorOptions; // Module to control application life. const app = electron.app; @@ -139,43 +140,59 @@ function createWindow(): Electron.BrowserWindow | undefined { const window = new electron.BrowserWindow(windowProperties); windows.push(window); - ElectronBlocker.fromPrebuiltAdsAndTracking(fetch) - .then( + // tslint:disable-next-line:no-floating-promises + ElectronBlocker.fromLists( + fetch, + [ + 'https://easylist.to/easylist/easylist.txt', + 'https://easylist.to/easylist/easyprivacy.txt', // EasyPrivacy + 'https://easylist.to/easylist/fanboy-social.txt', // Fanboy Social + 'https://easylist.to/easylist/fanboy-annoyance.txt', // Fanboy Annoyances + 'https://filters.adtidy.org/extension/chromium/filters/2.txt', // AdGuard Base + 'https://filters.adtidy.org/extension/chromium/filters/11.txt', // AdGuard Mobile Ads + 'https://filters.adtidy.org/extension/chromium/filters/4.txt', // AdGuard Social Media + 'https://filters.adtidy.org/extension/chromium/filters/14.txt', // AdGuard Annoyances + 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt', // uBlock Origin Filters + 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/privacy.txt', // uBlock Origin Privacy + 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/resource-abuse.txt' // uBlock Origin Resource Abuse + ] + ).then( (blocker) => { blocker.enableBlockingInSession(electron.session.defaultSession); - console.log('Got this far!!!!'); + // console.log('Got this far!!!!'); blocker.on('request-blocked', (request: Request) => { console.log('blocked', request.url); - }); + }); - blocker.on('request-redirected', (request: Request) => { + blocker.on('request-redirected', (request: Request) => { console.log('redirected', request.url); - }); + }); - blocker.on('request-whitelisted', (request: Request) => { + blocker.on('request-whitelisted', (request: Request) => { console.log('whitelisted', request.url); - }); + }); - blocker.on('csp-injected', (request: Request) => { + blocker.on('csp-injected', (request: Request) => { console.log('csp', request.url); - }); + }); - blocker.on('script-injected', (script: string, url: string) => { + blocker.on('script-injected', (script: string, url: string) => { console.log('script', script.length, url); - }); + }); - blocker.on('style-injected', (style: string, url: string) => { + blocker.on('style-injected', (style: string, url: string) => { console.log('style', style.length, url); - }); + }); } ); + // tslint:disable-next-line:no-floating-promises window.loadFile( path.join(__dirname, 'window.html'), { - query: {settings: JSON.stringify(settings), import: shouldImportSettings ? 'true' : []} + query: {settings: JSON.stringify(settings), import: shouldImportSettings ? 'true' : ''} } ); @@ -269,7 +286,7 @@ function onReady(): void { ] }; if(process.env.NODE_ENV !== 'production') - viewItem.submenu.unshift({role: 'reload'}, {role: 'forcereload'}, {role: 'toggledevtools'}, {type: 'separator'}); + viewItem.submenu.unshift({role: 'reload'}, {role: 'forceReload'}, {role: 'toggleDevTools'}, {type: 'separator'}); const spellcheckerMenu = new electron.Menu(); //tslint:disable-next-line:no-floating-promises addSpellcheckerItems(spellcheckerMenu); @@ -293,12 +310,12 @@ function onReady(): void { { label: l('settings.logDir'), click: (_, window: BrowserWindow) => { - const dir = electron.dialog.showOpenDialog( + const dir = electron.dialog.showOpenDialogSync( {defaultPath: settings.logDirectory, properties: ['openDirectory']}); if(dir !== undefined) { if(dir[0].startsWith(path.dirname(app.getPath('exe')))) return electron.dialog.showErrorBox(l('settings.logDir'), l('settings.logDir.inAppDir')); - const button = electron.dialog.showMessageBox(window, { + const button = electron.dialog.showMessageBoxSync(window, { message: l('settings.logDir.confirm', dir[0], settings.logDirectory), buttons: [l('confirmYes'), l('confirmNo')], cancelId: 1 @@ -359,7 +376,7 @@ function onReady(): void { label: l('action.quit'), click(_: Electron.MenuItem, window: Electron.BrowserWindow): void { if(characters.length === 0) return app.quit(); - const button = electron.dialog.showMessageBox(window, { + const button = electron.dialog.showMessageBoxSync(window, { message: l('chat.confirmLeave'), buttons: [l('confirmYes'), l('confirmNo')], cancelId: 1 @@ -370,7 +387,7 @@ function onReady(): void { } } } - ] + ] as MenuItemConstructorOptions[] }, { label: `&${l('action.edit')}`, submenu: [ @@ -381,7 +398,7 @@ function onReady(): void { {role: 'copy'}, {role: 'paste'}, {role: 'selectall'} - ] + ] as MenuItemConstructorOptions[] }, viewItem, { label: `&${l('help')}`, submenu: [