mirror of
https://github.com/ION606/browser-chromium.git
synced 2026-05-14 22:26:56 +00:00
initial commit/backup
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
// save the original window.open function
|
||||
const originalWindowOpen = window.open;
|
||||
|
||||
// override the window.open function
|
||||
window.open = function (url, target, features) {
|
||||
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);
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @param { @param {Electron.BrowserWindow} window} window
|
||||
*/
|
||||
export async function changeZoom(window, zoomIn = true, reset = false) {
|
||||
let zl = window.webContents.getZoomLevel();
|
||||
|
||||
if (reset) zl = 0
|
||||
else if (zoomIn) zl++;
|
||||
else zl--;
|
||||
|
||||
window.webContents.setZoomLevel(zl);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
async function waitForVideo() {
|
||||
return new Promise(resolve => {
|
||||
const i = setInterval(() => {
|
||||
if (document.querySelector('video')) {
|
||||
clearInterval(i);
|
||||
resolve(true);
|
||||
}
|
||||
}, 500)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function revertQuality() {
|
||||
setQuality(document?.body?.dataset?.oldel || 'auto');
|
||||
delete document?.body?.dataset?.oldel;
|
||||
}
|
||||
|
||||
|
||||
async function setQuality(quality = 'lowest') {
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
await waitForVideo();
|
||||
console.info(`changing quality to ${quality}`);
|
||||
|
||||
const qualityMenuMain = [document.querySelector('.ytp-settings-menu'), document.querySelector('.ytp-panel-menu')],
|
||||
settingsButton = document.querySelector('.ytp-settings-button');
|
||||
|
||||
qualityMenuMain.map(el => el.style.opacity = '0');
|
||||
settingsButton.click();
|
||||
await sleep(500);
|
||||
|
||||
const qualityMenu = document.querySelector('.ytp-panel-menu').lastElementChild;
|
||||
qualityMenu.click();
|
||||
await sleep(500);
|
||||
|
||||
const qualityOptions = [...document.querySelector('.ytp-panel.ytp-quality-menu').querySelectorAll('.ytp-menuitem')];
|
||||
let selection;
|
||||
|
||||
if (quality === 'lowest') selection = qualityOptions.findLast(el => (!el.textContent.toLowerCase().includes('auto')));
|
||||
else selection = qualityOptions.find((el) => el.textContent.toLowerCase().includes(quality));
|
||||
|
||||
const currentQuality = qualityOptions.find(el => el.ariaChecked);
|
||||
if (currentQuality) document.body.dataset.oldel = currentQuality.textContent.toLowerCase();
|
||||
|
||||
if (!selection) {
|
||||
let qualityTexts = qualityOptions.map((el) => el.textContent).join('\n');
|
||||
console.info('"' + quality + '" not found. Options are: \n\nHighest\n' + qualityTexts + '\n' + 'setting to auto');
|
||||
settingsButton.click(); // click the menu button to close it
|
||||
selection = qualityOptions.findLast(el => (el.textContent.toLowerCase().includes('auto')));
|
||||
}
|
||||
|
||||
selection.click();
|
||||
qualityMenuMain.map(el => el.style.opacity = '1');
|
||||
}
|
||||
|
||||
|
||||
function optimize() {
|
||||
sessionStorage.setItem('ran-optimize', 1);
|
||||
console.info('quality tuning script loaded!');
|
||||
// setQuality('Highest');
|
||||
}
|
||||
|
||||
if (document.readyState === 'complete' && !sessionStorage.getItem('ran-optimize')) optimize();
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron');
|
||||
|
||||
|
||||
// renderer.js or script loaded in the renderer process
|
||||
const policy = window.trustedTypes.createPolicy('default', {
|
||||
createHTML: (input) => input, // policy allows only sanitized HTML strings
|
||||
});
|
||||
|
||||
process.once("loaded", () => {
|
||||
console.info('injecting...');
|
||||
contextBridge.exposeInMainWorld('safeHTML', {
|
||||
ping: () => console.info('pong'),
|
||||
write: (selector, htmlString) => {
|
||||
const element = document.querySelector(selector);
|
||||
if (element) element.innerHTML += policy.createHTML(htmlString);
|
||||
},
|
||||
insertBefore: (selector, htmlString) => {
|
||||
const element = document.querySelector(selector);
|
||||
if (element) element.innerHTML = policy.createHTML(htmlString) + element.innerHTML;
|
||||
},
|
||||
addScript: (content, src) => {
|
||||
// validate and securely add the script to the document's head
|
||||
const head = document.head;
|
||||
if (!head || (!src && !content)) return;
|
||||
|
||||
// create a script element
|
||||
const script = document.createElement('script');
|
||||
|
||||
if (content) script.innerText = content;
|
||||
else script.src = src;
|
||||
|
||||
// set script properties securely
|
||||
script.async = true;
|
||||
|
||||
// append to head
|
||||
head.appendChild(script);
|
||||
},
|
||||
addStylesheet: (href, inlineContent) => {
|
||||
const head = document.head;
|
||||
if (!head) return;
|
||||
|
||||
if (href) {
|
||||
// if href is provided, use it for external stylesheet
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = href;
|
||||
head.appendChild(link);
|
||||
} else if (inlineContent) {
|
||||
// create a style element for inline CSS
|
||||
const style = document.createElement('style');
|
||||
style.textContent = inlineContent;
|
||||
head.appendChild(style);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// TODO: replaceme
|
||||
const uid = 1;
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
displayHistory: () => ipcRenderer.send('display-history', uid), // send message to main process
|
||||
getHistory: () => ipcRenderer.invoke('get-history', uid), // request history data and wait for response
|
||||
showHistory: (history) => ipcRenderer.send('show-history', history), // sends data to the main process
|
||||
initTabs: () => ipcRenderer.send('init-tabs'),
|
||||
addTab: (url) => window.dispatchEvent(new CustomEvent('add-tab', { detail: url })),
|
||||
sendToMain: (channel, data) => {
|
||||
ipcRenderer.send(channel, data);
|
||||
},
|
||||
onReceive: (channel, func) => {
|
||||
ipcRenderer.on(channel, (event, ...args) => func(...args));
|
||||
},
|
||||
checkperms: (sitehostname) => ipcRenderer.send('get-site-perms', sitehostname)
|
||||
});
|
||||
|
||||
// ipcRenderer.on('tab-opened', (ev, id) => {
|
||||
// document.querySelector('.open-webview')?.classList.remove('open-webview');
|
||||
// document.querySelector(`#webview-${id}`)?.classList.add('open-webview');
|
||||
// });
|
||||
|
||||
ipcRenderer.on('tab-created', (ev, id, url = 'https://duckduckgo.com') => createWebview(url, id));
|
||||
|
||||
|
||||
contextBridge.exposeInMainWorld('tabAPI', {
|
||||
ping: () => console.info('pong'),
|
||||
addTab: (url) => ipcRenderer.send('add-tab', url || 'about:blank')
|
||||
});
|
||||
|
||||
const load = () => {
|
||||
console.info("PRELOAD LOADED!");
|
||||
|
||||
if (window.location.origin === 'lite.duckduckgo.com') {
|
||||
document.body.querySelector('img[src="//duckduckgo.com/t/sl_l"]').remove();
|
||||
}
|
||||
|
||||
if (document.body) {
|
||||
// document.body.innerHTML = `<webview src="../HTML/tabs.html" style="flex:0 0.5 auto;" nodeintegration preload="../organization/tabs.cjs"></webview>` + document.body.innerHTML;
|
||||
ipcRenderer.send('init-tabs');
|
||||
}
|
||||
}
|
||||
|
||||
// document.onload = () => load;
|
||||
document.addEventListener('DOMContentLoaded', load);
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
function runinit() {
|
||||
console.info("added renderer script!");
|
||||
sessionStorage.setItem('ran-renderer', 1);
|
||||
forceDarkMode();
|
||||
}
|
||||
|
||||
|
||||
var removehbar = removehbar || function (e) {
|
||||
const hbar = document.querySelector('#historybar');
|
||||
if (!hbar || hbar.contains(e?.target)) return;
|
||||
|
||||
// slide out by setting margin-left to -100%
|
||||
hbar.style.marginLeft = '-100%';
|
||||
setTimeout(() => hbar.remove(), 500);
|
||||
|
||||
document.removeEventListener('click', removehbar);
|
||||
}
|
||||
|
||||
|
||||
function showHistory(...h) {
|
||||
if (Array.isArray(h[0])) h = h.flat(1);
|
||||
const hbar = document.querySelector('#historybar');
|
||||
if (hbar) return removehbar();
|
||||
|
||||
setTimeout(() => document.addEventListener('click', removehbar), 1000);
|
||||
|
||||
const sidebar = document.createElement('div');
|
||||
sidebar.id = 'historybar';
|
||||
sidebar.style.marginLeft = '-100%'; // start off-screen
|
||||
|
||||
const t = document.createElement('table'),
|
||||
tbody = document.createElement('tbody');
|
||||
|
||||
t.appendChild(tbody);
|
||||
|
||||
h.forEach(iraw => {
|
||||
const i = (typeof iraw === 'string') ? JSON.parse(iraw) : iraw,
|
||||
el = document.createElement('tr'),
|
||||
el2 = document.createElement('td'),
|
||||
a = document.createElement('a');
|
||||
|
||||
a.textContent = i.title;
|
||||
a.href = i.query;
|
||||
a.style.width = '100%';
|
||||
a.style.height = '100%';
|
||||
|
||||
el2.appendChild(a);
|
||||
el.appendChild(el2);
|
||||
tbody.appendChild(el);
|
||||
});
|
||||
|
||||
sidebar.appendChild(t);
|
||||
document.body.appendChild(sidebar);
|
||||
|
||||
// force reflow to apply the transition correctly
|
||||
window.getComputedStyle(sidebar).marginLeft;
|
||||
|
||||
// slide in by setting margin-left to 0
|
||||
sidebar.style.marginLeft = '0';
|
||||
}
|
||||
|
||||
|
||||
function forceDarkMode() {
|
||||
document.querySelector('[value="night"]')?.click(); // wikipedia
|
||||
|
||||
// manual
|
||||
// function to convert rgb values to a hex string
|
||||
const rgbToHex = (r, g, b) => {
|
||||
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
|
||||
}
|
||||
|
||||
// function to get the computed background and text color of the body
|
||||
const checkColors = () => {
|
||||
const body = document.querySelector('body') || document.querySelector('main') // select the body element
|
||||
const bgColor = window.getComputedStyle(body).backgroundColor // get background color
|
||||
const textColor = window.getComputedStyle(body).color // get text color
|
||||
|
||||
// parse the rgb values from the background and text color
|
||||
const bgMatch = bgColor.match(/\d+/g)
|
||||
const textMatch = textColor.match(/\d+/g)
|
||||
|
||||
// if both colors are in rgb format
|
||||
if (bgMatch && textMatch) {
|
||||
const bgHex = rgbToHex(parseInt(bgMatch[0]), parseInt(bgMatch[1]), parseInt(bgMatch[2]))
|
||||
const textHex = rgbToHex(parseInt(textMatch[0]), parseInt(textMatch[1]), parseInt(textMatch[2]))
|
||||
|
||||
// check if background is white (#FFFFFF) and text is dark (let's assume below #777777 as dark)
|
||||
if (bgHex === "#FFFFFF" && textHex <= "#777777") {
|
||||
// swap the background to dark and text to light
|
||||
body.style.backgroundColor = "#000000" // set background to black
|
||||
body.style.color = "#FFFFFF" // set text color to white
|
||||
}
|
||||
}
|
||||
}
|
||||
checkColors();
|
||||
}
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', runinit);
|
||||
if (document.readyState === 'complete' && !sessionStorage.getItem('ran-renderer')) runinit();
|
||||
Reference in New Issue
Block a user