added permissions for website

This commit is contained in:
2024-11-04 11:55:14 -05:00
parent 5c845ffe61
commit 014e4c8b7b
19 changed files with 507 additions and 71 deletions
+63 -8
View File
@@ -1,15 +1,70 @@
// save the original window.open function
const originalWindowOpen = window.open;
// override the window.open function
window.open = function (url, target, features) {
function checkAdBlock(url, target, features, originalWindowOpen) {
console.log('A new window is attempting to open:');
console.log('URL:', url);
console.log('Target:', target);
console.log('Features:', features);
window.electronAPI.checkperms(window.location.hostname);
// call the original window.open function if you want the popup to proceed
return originalWindowOpen.call(window, url, target, features);
};
}
// needs to be inside the function or it'll get assigned twice
async function setupAdBlock(perms) {
// attach listeners to the document body for each event type
['click', 'mousedown', 'mouseup', 'dblclick', 'keydown', 'keyup', 'submit'].forEach((eventType) => {
document.body.addEventListener(eventType, (e) => {
console.log(`Event ${e.type} triggered by:`, e.target);
}, true); // use capture phase
});
const originalWindowOpen = window.open,
isValidURL = (u) => { try { return new URL(u); } catch (_) { return false; } }
window.open = (url, target, features) => {
if (!perms['popup']) return window.electronAPI.promptperms();
checkAdBlock(url, target, features, originalWindowOpen);
}
const linkels = Array.from(document.links),
imgs = Array.from(document.querySelectorAll('img')),
allLinks = new Set(linkels.map(el => el.href).filter(isValidURL)),
imgsrcs = new Set(imgs.map(o => o.src));
await fetch(`https://ion-adblock.${window.location.hostname}`, {
body: JSON.stringify([...allLinks]), //['https://pagead2.googlesyndication.com/tag/js/gpt.js']
headers: { 'Content-Type': 'application/json' },
method: 'POST'
})
.then(r => r.json()).then(rj => {
const resjson = rj.filter(el => el[1]),
obj = Object.fromEntries(resjson);
linkels.forEach(async (el) => {
if (obj[el.href]) el.remove();
});
});
await fetch(`https://ion-adblock.${window.location.hostname}`, {
body: JSON.stringify([...imgsrcs]), //['https://pagead2.googlesyndication.com/tag/js/gpt.js']
headers: { 'Content-Type': 'application/json' },
method: 'POST'
})
.then(r => r.json()).then(rj => {
const resjson = rj.filter(el => el[1]),
obj = Object.fromEntries(resjson);
console.log(obj);
imgs.forEach(async (el) => {
if (obj[el.src]) el.remove();
});
});
sessionStorage.setItem('ran-adblock', 1);
console.log('adblock injected!');
}
// if (document.readyState === 'complete' && !sessionStorage.getItem('ran-adblock')) setupAdBlock();
// else window.addEventListener('DOMContentLoaded', setupAdBlock);
+1 -1
View File
@@ -61,4 +61,4 @@ function optimize() {
}
if (document.readyState === 'complete' && !sessionStorage.getItem('ran-optimize')) optimize();
else window.addEventListener('DOMContentLoaded', optimize);
+37
View File
@@ -0,0 +1,37 @@
const { ipcRenderer } = require("electron");
ipcRenderer.on('conf', (e, id) => {
document.querySelector(id).style.borderColor = 'green';
})
window.onbeforeunload = () => window.close();
document.addEventListener('DOMContentLoaded', () => {
const title = new URLSearchParams(window.location.search).get('origin');
document.querySelector('#sitename').textContent = title;
ipcRenderer.send('get-site-perms', title);
ipcRenderer.on('site-perms', (e, permsRaw) => {
const perms = JSON.parse(permsRaw);
if (!perms) return ipcRenderer.send('set-site-perms-all', title, 'ask');
document.querySelector('#loading').style.display = 'none';
for (const key in perms) {
const el = document.querySelector(`#${key}`);
if (el.value === perms[key]) el.style.border = 'solid green 1px';
el.value = perms[key];
}
});
document.querySelectorAll('select').forEach((el) => {
el.addEventListener('change', (e) => {
e.preventDefault();
e.target.style.border = 'none';
document.querySelector('#loading').style.display = 'flex';
const { id, value } = e.target;
ipcRenderer.send('set-site-perms', title, id, value);
});
});
});
+3 -2
View File
@@ -71,7 +71,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
onReceive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(...args));
},
checkperms: (sitehostname) => ipcRenderer.send('get-site-perms', sitehostname)
checkperms: (sitehostname) => ipcRenderer.send('get-site-perms', sitehostname),
promptperms: () => ipcRenderer.send('prompt-terms', window.location.hostname)
});
// ipcRenderer.on('tab-opened', (ev, id) => {
@@ -87,7 +88,7 @@ contextBridge.exposeInMainWorld('tabAPI', {
addTab: (url) => ipcRenderer.send('add-tab', url || 'about:blank')
});
const load = () => {
const load = async () => {
console.info("PRELOAD LOADED!");
if (window.location.origin === 'lite.duckduckgo.com') {