GTK4: fix duplicate loading error when opening new tabs

This commit is contained in:
NunoSempere 2024-07-21 14:54:32 -04:00
parent e03b6cc447
commit db9d3e66f2
4 changed files with 92 additions and 91 deletions

View File

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define STYLE_N 8049 + 1000 #define STYLE_N 7877 + 1000
void read_style_js(char* string) void read_style_js(char* string)
{ {

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#define STYLE_N 8049 + 1000 #define STYLE_N 7877 + 1000
void read_style_js(char* string); void read_style_js(char* string);

View File

@ -6,8 +6,8 @@
var styles = null; var styles = null;
// console.log(document.domain); // console.log(document.domain);
switch (document.domain) { switch (document.domain) {
case "forum.effectivealtruism.org": case "forum.effectivealtruism.org":
styles = ` styles = `
/* /*
.Layout-main { .Layout-main {
margin-left: 100px; margin-left: 100px;
@ -25,16 +25,16 @@ switch (document.domain) {
} }
*/ */
`; `;
break; break;
case "nationstates.net": case "nationstates.net":
styles = ` styles = `
.adidentifier { .adidentifier {
display: none; display: none;
} }
`; `;
break; break;
case "mail.proton.me": case "mail.proton.me":
styles = ` styles = `
/* /*
.item-container-row.read, .item-container.read { .item-container-row.read, .item-container.read {
background-color: white; background-color: white;
@ -48,24 +48,24 @@ switch (document.domain) {
zoom: 0.625 !important; zoom: 0.625 !important;
*/ */
`; `;
break; break;
case "forum.nunosempere.com": case "forum.nunosempere.com":
styles = ` styles = `
body { body {
zoom: 0.625 !important; zoom: 0.625 !important;
} }
`; `;
break; break;
case "search.brave.com": case "search.brave.com":
styles = ` styles = `
.download-button, .download-button,
a[href^="https://brave.com/download/"] { a[href^="https://brave.com/download/"] {
display: none !important; display: none !important;
} }
`; `;
break; break;
case "search.nunosempere.com": case "search.nunosempere.com":
styles = ` styles = `
/* /*
body { body {
zoom: 1.8; zoom: 1.8;
@ -76,11 +76,11 @@ switch (document.domain) {
display: none; display: none;
} }
`; `;
break; break;
case "reddit.com": case "reddit.com":
// fallthrough // fallthrough
case "old.reddit.com": case "old.reddit.com":
styles = ` styles = `
/* kill sidebar ads */ /* kill sidebar ads */
.ad-container, .ad-container,
a[href^="https://alb.reddit.com"] a[href^="https://alb.reddit.com"]
@ -98,10 +98,10 @@ switch (document.domain) {
display: none !important; display: none !important;
} }
`; `;
break; break;
case "twitter.com": case "twitter.com":
case "x.com": case "x.com":
styles = ` styles = `
/* hide promoted tweets */ /* hide promoted tweets */
:has(meta[property="og:site_name"][content="Twitter"]) :has(meta[property="og:site_name"][content="Twitter"])
[data-testid="cellInnerDiv"]:has(svg + [dir="auto"]) { [data-testid="cellInnerDiv"]:has(svg + [dir="auto"]) {
@ -182,25 +182,25 @@ switch (document.domain) {
background: white !important; background: white !important;
} }
`; `;
break; break;
default: default:
console.log(`Domain: ${document.domain}`); console.log(`Domain: ${document.domain}`);
console.log("No custom style"); console.log("No custom style");
} }
if (styles != null) { if (styles != null) {
var styleSheet = document.createElement("style"); var styleSheet = document.createElement("style");
styleSheet.innerText = styles; styleSheet.innerText = styles;
document.head.appendChild(styleSheet); document.head.appendChild(styleSheet);
console.log("Style changed"); console.log("Style changed");
} }
// Extra: Replace default alert with new function // Extra: Replace default alert with new function
// whose style can be changed! // whose style can be changed!
window.alert = (message) => { window.alert = (message) => {
let alertDiv = document.getElementById("customAlert"); let alertDiv = document.getElementById("customAlert");
if (!alertDiv) { if (!alertDiv) {
const html = ` const html = `
<div id="customAlert" class="custom-alert"> <div id="customAlert" class="custom-alert">
<div class="custom-alert-content"> <div class="custom-alert-content">
<p id="alertMessage"></p> <p id="alertMessage"></p>
@ -232,68 +232,68 @@ window.alert = (message) => {
} }
</style> </style>
`; `;
document.body.insertAdjacentHTML("beforeend", html); document.body.insertAdjacentHTML("beforeend", html);
alertDiv = document.getElementById("customAlert"); alertDiv = document.getElementById("customAlert");
document.getElementById("alertOkButton").onclick = () => { document.getElementById("alertOkButton").onclick = () => {
alertDiv.classList.remove("visible"); alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert); document.removeEventListener("keydown", dismissAlert);
}; };
} }
const dismissAlert = (event) => { const dismissAlert = (event) => {
if ( if (
event.key === "Enter" /*&& event.ctrlKey*/ && event.key === "Enter" /*&& event.ctrlKey*/ &&
alertDiv.classList.contains("visible") alertDiv.classList.contains("visible")
) { ) {
alertDiv.classList.remove("visible"); alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert); document.removeEventListener("keydown", dismissAlert);
} }
}; };
document.addEventListener("keydown", dismissAlert); document.addEventListener("keydown", dismissAlert);
document.getElementById("alertMessage").textContent = message; document.getElementById("alertMessage").textContent = message;
alertDiv.classList.add("visible"); alertDiv.classList.add("visible");
}; };
// Extra: hide video players on twitter // Extra: hide video players on twitter
if (document.domain == "twitter.com" || document.domain == "x.com") { if (document.domain == "twitter.com" || document.domain == "x.com") {
// Function to hide the grandparent of video players // Function to hide the grandparent of video players
// takes 0.014ms to run, so performance is not the concern here. // takes 0.014ms to run, so performance is not the concern here.
// timed with console.time, console.timeEnd // timed with console.time, console.timeEnd
function hideVideoPlayerGrandparent() { function hideVideoPlayerGrandparent() {
document document
.querySelectorAll('[data-testid="videoPlayer"]') .querySelectorAll('[data-testid="videoPlayer"]')
.forEach(function (videoPlayer) { .forEach(function (videoPlayer) {
var grandparentElement = var grandparentElement =
videoPlayer.parentElement.parentElement.parentElement.parentElement videoPlayer.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement; .parentElement.parentElement;
var newTextElement = document.createElement("div"); var newTextElement = document.createElement("div");
newTextElement.textContent = " [ twitter video ] "; newTextElement.textContent = " [ twitter video ] ";
newTextElement.style["margin-top"] = "10px"; newTextElement.style["margin-top"] = "10px";
newTextElement.style["margin-left"] = "10px"; newTextElement.style["margin-left"] = "10px";
newTextElement.style["margin-bottom"] = "10px"; newTextElement.style["margin-bottom"] = "10px";
grandparentElement.replaceWith(newTextElement); grandparentElement.replaceWith(newTextElement);
}); });
} }
// Create a new MutationObserver instance // Create a new MutationObserver instance
var observer = new MutationObserver(function (mutations) { var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) { mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) { if (mutation.addedNodes.length) {
hideVideoPlayerGrandparent(); // Call the function to hide video players hideVideoPlayerGrandparent(); // Call the function to hide video players
} }
}); });
}); });
// Options for the observer (which mutations to observe) // Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true }; var config = { childList: true, subtree: true };
// Start observing the target node for configured mutations // Start observing the target node for configured mutations
observer.observe(document.body, config); observer.observe(document.body, config);
// Call the function initially to hide any video players on initial load // Call the function initially to hide any video players on initial load
hideVideoPlayerGrandparent(); hideVideoPlayerGrandparent();
} }
// document.body.style.visibility = "visible"; // document.body.style.visibility = "visible";

View File

@ -166,13 +166,14 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
if (num_tabs < MAX_NUM_TABS || num_tabs == 0) { if (num_tabs < MAX_NUM_TABS || num_tabs == 0) {
WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action); WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action);
const char* uri = webkit_uri_request_get_uri(uri_request); const char* uri = webkit_uri_request_get_uri(uri_request);
webkit_web_view_stop_loading(self);
printf("Creating new window: %s\n", uri); printf("Creating new window: %s\n", uri);
notebook_create_new_tab(notebook, uri); notebook_create_new_tab(notebook, uri);
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
} else { } else {
webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
} }
return GTK_WIDGET(self); // or NULL return NULL; // Could also return GTK_WIDGET(self), in which case the new uri would also be loaded in the current webview.
} }
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri) void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)