Skip to content

Instantly share code, notes, and snippets.

@michael-dm
Created January 15, 2023 10:20
Show Gist options
  • Save michael-dm/bf00689b195a6464b2f21fd6e9671595 to your computer and use it in GitHub Desktop.
Save michael-dm/bf00689b195a6464b2f21fd6e9671595 to your computer and use it in GitHub Desktop.
electron adblocker bug
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
<style>
body, html, webview {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<!-- We will get an ad on this video when opening the app,
Any video loaded later won't. -->
<webview src="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></webview>
<script></script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, session} = require('electron')
const path = require('path')
const { ElectronBlocker, fullLists } = require('@cliqz/adblocker-electron')
const fetch = require('cross-fetch')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
webviewTag: true,
sandbox: false,
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(async () => {
//Setting up adblocker
const blocker = await ElectronBlocker.fromLists(fetch, fullLists, {
enableCompression: true,
})
blocker.enableBlockingInSession(session.defaultSession)
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "electron-adblock-bug",
"productName": "electron-adblock-bug",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "midm",
"scripts": {
"start": "electron ."
},
"dependencies": {
"@cliqz/adblocker-electron": "1.25.1",
"cross-fetch": "3.1.5"
},
"devDependencies": {
"electron": "20.1.4"
}
}
/**
* The preload script runs before. It has access to web APIs
* as well as Electron's renderer process modules and some
* polyfilled Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment