Fixes to image preview

This commit is contained in:
Mr. Stallion 2020-04-01 19:27:21 -05:00
parent dfb50d731f
commit 155e1b659e
6 changed files with 109 additions and 104 deletions

View File

@ -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();

View File

@ -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');
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());
// 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());
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));
}
.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);
}

View File

@ -237,27 +237,24 @@ export class ImagePreviewMutator {
];
const resolveImgSize = function() {
return sizePairs.reduce(
(acc, val) => {
if ((acc.width) && (acc.height)) {
return acc;
}
const solved = {};
for (let ri = 0; ri < sizePairs.length; ri++) {
const val = sizePairs[ri];
if ((img[val[0]]) && (img[val[1]])) {
return {
width: img[val[0]],
height: img[val[1]]
solved.width = img[val[0]];
solved.height = img[val[1]];
break;
}
}
return acc;
},
{}
);
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(',')}];

View File

@ -85,7 +85,7 @@
@Hook('mounted')
async mounted(): Promise<void> {
// top bar devtools
browserWindow.webContents.openDevTools( { mode: 'detach' } );
// browserWindow.webContents.openDevTools({ mode: 'detach' });
await this.addTab();

View File

@ -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;

View File

@ -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,12 +140,27 @@ 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);
@ -172,10 +188,11 @@ function createWindow(): Electron.BrowserWindow | undefined {
}
);
// 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: [