Compare commits

...

7 Commits

11 changed files with 117 additions and 86 deletions

View File

@ -16,7 +16,6 @@
// Plugins // Plugins
#define LIBRE_REDIRECT_ENABLED true #define LIBRE_REDIRECT_ENABLED true
#define READABILITY_ENABLED true #define READABILITY_ENABLED true
#define CUSTOM_STYLE_ENABLED true
#define CUSTOM_USER_AGENT false #define CUSTOM_USER_AGENT false
/* /*
To disable plugins: To disable plugins:
@ -51,25 +50,33 @@ You could also look into commit afe93518a for an approach using stand-in code.
// Shortcuts // Shortcuts
typedef enum { typedef enum {
goback, goback,
goforward, goforward,
refresh,
refresh_force, refresh,
back_to_home, refresh_force,
toggle_fullscreen,
zoomin, back_to_home,
zoomout,
zoom_reset, toggle_fullscreen,
new_tab, toggle_custom_style,
next_tab,
prev_tab, zoomin,
close_tab, zoomout,
show_searchbar, zoom_reset,
show_finder,
finder_next, new_tab,
finder_prev, next_tab,
prettify, prev_tab,
hide_bar close_tab,
show_searchbar,
hide_bar,
show_finder,
finder_next,
finder_prev,
prettify,
} func; } func;
@ -78,27 +85,35 @@ static struct {
unsigned key; unsigned key;
func id; func id;
} shortcut[] = { } shortcut[] = {
{ CTRL, KEY(h), goback }, { CTRL, KEY(h), goback },
{ CTRL, KEY(j), goforward }, { CTRL, KEY(j), goforward },
{ CTRL, KEY(r), refresh },
{ CTRL, KEY(R), refresh_force }, { CTRL, KEY(r), refresh },
{ CTRL, KEY(H), back_to_home }, { CTRL, KEY(R), refresh_force },
{ CTRL, KEY(equal), zoomin },
{ CTRL, KEY(minus), zoomout }, { CTRL, KEY(H), back_to_home },
{ CTRL, KEY(0), zoom_reset },
{ CTRL, KEY(KP_Page_Up), prev_tab }, { 0x0, KEY(F11), toggle_fullscreen },
{ CTRL, KEY(KP_Page_Down), next_tab }, { CTRL, KEY(S), toggle_custom_style },
{ CTRL, KEY(Page_Up), prev_tab }, // working hypothesis: Page_UP vs KP_Page_Up might depend on whether the user has a numpad
{ CTRL, KEY(Page_Down), next_tab }, { CTRL, KEY(equal), zoomin },
{ CTRL, KEY(t), new_tab }, { CTRL, KEY(minus), zoomout },
{ CTRL, KEY(w), close_tab }, { CTRL, KEY(0), zoom_reset },
{ 0x0, KEY(F11), toggle_fullscreen },
{ CTRL, KEY(l), show_searchbar }, { CTRL, KEY(KP_Page_Up), prev_tab },
{ CTRL, KEY(semicolon), hide_bar }, { CTRL, KEY(KP_Page_Down), next_tab },
{ CTRL, KEY(f), show_finder }, { CTRL, KEY(Page_Up), prev_tab }, // working hypothesis: Page_UP vs KP_Page_Up might depend on whether the user has a numpad
{ CTRL, KEY(n), finder_next }, { CTRL, KEY(Page_Down), next_tab },
{ CTRL, KEY(N), finder_prev }, { CTRL, KEY(t), new_tab },
{ CTRL, KEY(p), prettify } { CTRL, KEY(w), close_tab },
{ CTRL, KEY(l), show_searchbar },
{ CTRL, KEY(semicolon), hide_bar },
{ CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next },
{ CTRL, KEY(N), finder_prev },
{ CTRL, KEY(p), prettify }
}; };
/* Old controls: { /* Old controls: {

View File

@ -22,7 +22,8 @@ ADBLOCK='-L/usr/lib/wyebrowser/adblock.so' # optional adblocking; depends on htt
## Formatter ## Formatter
STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: true, AllowShortEnumsOnASingleLine: true}" STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: true, AllowShortEnumsOnASingleLine: true}"
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) FORMATTER_C=clang-format -i -style=$(STYLE_BLUEPRINT)
FORMATTER_JS=npx prettier -w
# Runtime files # Runtime files
MAINTAINER_CACHE_DIR=/home/nuno/.cache/rosenrot MAINTAINER_CACHE_DIR=/home/nuno/.cache/rosenrot
@ -68,7 +69,9 @@ clean:
rm $(USER_CACHE_DIR) rm $(USER_CACHE_DIR)
format: $(SRC) $(PLUGINS) format: $(SRC) $(PLUGINS)
$(FORMATTER) $(SRC) $(PLUGINS) $(rosenrot.h) $(FORMATTER_C) $(SRC) $(PLUGINS) $(rosenrot.h)
$(FORMATTER_JS) plugins/readability/readability.js
$(FORMATTER_JS) plugins/style/style.js
lint: lint:
clang-tidy $(SRC) $(PLUGINS) -- -Wall -O3 $(INCS) -o rosenrot $(LIBS) clang-tidy $(SRC) $(PLUGINS) -- -Wall -O3 $(INCS) -o rosenrot $(LIBS)

View File

@ -17,6 +17,7 @@ int libre_redirect(const char* uri, char* output)
} else { } else {
char* annoying_sites[] = { char* annoying_sites[] = {
"https://www.reddit.com", "https://www.reddit.com",
"https://vitalik.ca",
"https://www.youtube.com", "https://www.youtube.com",
"https://google.com", "https://google.com",
"https://medium.com", "https://medium.com",
@ -29,12 +30,15 @@ int libre_redirect(const char* uri, char* output)
}; };
char* alternatives[] = { char* alternatives[] = {
"https://old.reddit.com", // "https://teddit.nunosempere.com", "https://redlib.ducks.party",
// previously: "https://old.reddit.com", "https://teddit.nunosempere.com",
// https://github.com/redlib-org/redlib-instances/blob/main/instances.md
"https://vitalik.eth.limo",
"https://invidious.private.coffee", "https://invidious.private.coffee",
"https://search.nunosempere.com", "https://search.nunosempere.com",
"https://scribe.rip", "https://scribe.rip",
"https://translate.riverside.rocks", "https://translate.riverside.rocks",
"https://archive.is/https://www.bloomberg.com", "https://archive.ph/https://www.bloomberg.com",
"https://royalread.nunosempere.com", "https://royalread.nunosempere.com",
"https://dumb.vern.cc", "https://dumb.vern.cc",
// "https://wayback.nunosempere.com", // "https://wayback.nunosempere.com",

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 READABILITY_N 88067 + 1000 #define READABILITY_N 88097 + 1000
void read_readability_js(char* string) void read_readability_js(char* string)
{ {

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#define READABILITY_N 88067 + 1000 #define READABILITY_N 88097 + 1000
void read_readability_js(char* string); void read_readability_js(char* string);

View File

@ -32,7 +32,7 @@ function Readability(doc, options) {
options = arguments[2]; options = arguments[2];
} else if (!doc || !doc.documentElement) { } else if (!doc || !doc.documentElement) {
throw new Error( throw new Error(
"First argument to Readability constructor should be a document object." "First argument to Readability constructor should be a document object.",
); );
} }
options = options || {}; options = options || {};
@ -53,7 +53,7 @@ function Readability(doc, options) {
options.nbTopCandidates || this.DEFAULT_N_TOP_CANDIDATES; options.nbTopCandidates || this.DEFAULT_N_TOP_CANDIDATES;
this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD; this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD;
this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat( this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(
options.classesToPreserve || [] options.classesToPreserve || [],
); );
this._keepClasses = !!options.keepClasses; this._keepClasses = !!options.keepClasses;
this._serializer = this._serializer =
@ -405,7 +405,7 @@ Readability.prototype = {
tagNames.map(function (tag) { tagNames.map(function (tag) {
var collection = node.getElementsByTagName(tag); var collection = node.getElementsByTagName(tag);
return Array.isArray(collection) ? collection : Array.from(collection); return Array.isArray(collection) ? collection : Array.from(collection);
}) }),
); );
}, },
@ -517,7 +517,7 @@ Readability.prototype = {
this.REGEXPS.srcsetUrl, this.REGEXPS.srcsetUrl,
function (_, p1, p2, p3) { function (_, p1, p2, p3) {
return toAbsoluteURI(p1) + (p2 || "") + p3; return toAbsoluteURI(p1) + (p2 || "") + p3;
} },
); );
media.setAttribute("srcset", newSrcset); media.setAttribute("srcset", newSrcset);
@ -545,7 +545,7 @@ Readability.prototype = {
for (var i = 0; i < node.attributes.length; i++) { for (var i = 0; i < node.attributes.length; i++) {
child.setAttribute( child.setAttribute(
node.attributes[i].name, node.attributes[i].name,
node.attributes[i].value node.attributes[i].value,
); );
} }
node.parentNode.replaceChild(child, node); node.parentNode.replaceChild(child, node);
@ -574,7 +574,7 @@ Readability.prototype = {
// If they had an element with id "title" in their HTML // If they had an element with id "title" in their HTML
if (typeof curTitle !== "string") if (typeof curTitle !== "string")
curTitle = origTitle = this._getInnerText( curTitle = origTitle = this._getInnerText(
doc.getElementsByTagName("title")[0] doc.getElementsByTagName("title")[0],
); );
} catch (e) { } catch (e) {
/* ignore exceptions setting the title. */ /* ignore exceptions setting the title. */
@ -599,7 +599,7 @@ Readability.prototype = {
// could assume it's the full title. // could assume it's the full title.
var headings = this._concatNodeLists( var headings = this._concatNodeLists(
doc.getElementsByTagName("h1"), doc.getElementsByTagName("h1"),
doc.getElementsByTagName("h2") doc.getElementsByTagName("h2"),
); );
var trimmedTitle = curTitle.trim(); var trimmedTitle = curTitle.trim();
var match = this._someNode(headings, function (heading) { var match = this._someNode(headings, function (heading) {
@ -755,7 +755,7 @@ Readability.prototype = {
try { try {
replacement.setAttribute( replacement.setAttribute(
node.attributes[i].name, node.attributes[i].name,
node.attributes[i].value node.attributes[i].value,
); );
} catch (ex) { } catch (ex) {
/* it's possible for setAttribute() to throw if the attribute name /* it's possible for setAttribute() to throw if the attribute name
@ -825,7 +825,7 @@ Readability.prototype = {
// replace H1 with H2 as H1 should be only title that is displayed separately // replace H1 with H2 as H1 should be only title that is displayed separately
this._replaceNodeTags( this._replaceNodeTags(
this._getAllNodesWithTag(articleContent, ["h1"]), this._getAllNodesWithTag(articleContent, ["h1"]),
"h2" "h2",
); );
// Remove extra paragraphs // Remove extra paragraphs
@ -840,7 +840,7 @@ Readability.prototype = {
var totalCount = imgCount + embedCount + objectCount + iframeCount; var totalCount = imgCount + embedCount + objectCount + iframeCount;
return totalCount === 0 && !this._getInnerText(paragraph, false); return totalCount === 0 && !this._getInnerText(paragraph, false);
} },
); );
this._forEachNode( this._forEachNode(
@ -848,7 +848,7 @@ Readability.prototype = {
function (br) { function (br) {
var next = this._nextNode(br.nextSibling); var next = this._nextNode(br.nextSibling);
if (next && next.tagName == "P") br.parentNode.removeChild(br); if (next && next.tagName == "P") br.parentNode.removeChild(br);
} },
); );
// Remove single-cell tables // Remove single-cell tables
@ -866,12 +866,12 @@ Readability.prototype = {
cell, cell,
this._everyNode(cell.childNodes, this._isPhrasingContent) this._everyNode(cell.childNodes, this._isPhrasingContent)
? "P" ? "P"
: "DIV" : "DIV",
); );
table.parentNode.replaceChild(cell, table); table.parentNode.replaceChild(cell, table);
} }
} }
} },
); );
}, },
@ -1032,7 +1032,7 @@ Readability.prototype = {
while (true) { while (true) {
this.log("Starting grabArticle loop"); this.log("Starting grabArticle loop");
var stripUnlikelyCandidates = this._flagIsActive( var stripUnlikelyCandidates = this._flagIsActive(
this.FLAG_STRIP_UNLIKELYS this.FLAG_STRIP_UNLIKELYS,
); );
// First, node prepping. Trash nodes that look cruddy (like ones with the // First, node prepping. Trash nodes that look cruddy (like ones with the
@ -1066,7 +1066,7 @@ Readability.prototype = {
this.log( this.log(
"Removing header: ", "Removing header: ",
node.textContent.trim(), node.textContent.trim(),
this._articleTitle.trim() this._articleTitle.trim(),
); );
shouldRemoveTitleHeader = false; shouldRemoveTitleHeader = false;
node = this._removeAndGetNext(node); node = this._removeAndGetNext(node);
@ -1093,7 +1093,7 @@ Readability.prototype = {
"Removing content with role " + "Removing content with role " +
node.getAttribute("role") + node.getAttribute("role") +
" - " + " - " +
matchString matchString,
); );
node = this._removeAndGetNext(node); node = this._removeAndGetNext(node);
continue; continue;
@ -1285,7 +1285,7 @@ Readability.prototype = {
0.75 0.75
) { ) {
alternativeCandidateAncestors.push( alternativeCandidateAncestors.push(
this._getNodeAncestors(topCandidates[i]) this._getNodeAncestors(topCandidates[i]),
); );
} }
} }
@ -1302,8 +1302,8 @@ Readability.prototype = {
) { ) {
listsContainingThisAncestor += Number( listsContainingThisAncestor += Number(
alternativeCandidateAncestors[ancestorIndex].includes( alternativeCandidateAncestors[ancestorIndex].includes(
parentOfTopCandidate parentOfTopCandidate,
) ),
); );
} }
if (listsContainingThisAncestor >= MINIMUM_TOPCANDIDATES) { if (listsContainingThisAncestor >= MINIMUM_TOPCANDIDATES) {
@ -1367,7 +1367,7 @@ Readability.prototype = {
var siblingScoreThreshold = Math.max( var siblingScoreThreshold = Math.max(
10, 10,
topCandidate.readability.contentScore * 0.2 topCandidate.readability.contentScore * 0.2,
); );
// Keep potential top candidate's parent node to try to get text direction of it later. // Keep potential top candidate's parent node to try to get text direction of it later.
parentOfTopCandidate = topCandidate.parentNode; parentOfTopCandidate = topCandidate.parentNode;
@ -1382,11 +1382,11 @@ Readability.prototype = {
sibling, sibling,
sibling.readability sibling.readability
? "with score " + sibling.readability.contentScore ? "with score " + sibling.readability.contentScore
: "" : "",
); );
this.log( this.log(
"Sibling has score", "Sibling has score",
sibling.readability ? sibling.readability.contentScore : "Unknown" sibling.readability ? sibling.readability.contentScore : "Unknown",
); );
if (sibling === topCandidate) { if (sibling === topCandidate) {
@ -1529,7 +1529,7 @@ Readability.prototype = {
if (parseSuccessful) { if (parseSuccessful) {
// Find out text direction from ancestors of final top candidate. // Find out text direction from ancestors of final top candidate.
var ancestors = [parentOfTopCandidate, topCandidate].concat( var ancestors = [parentOfTopCandidate, topCandidate].concat(
this._getNodeAncestors(parentOfTopCandidate) this._getNodeAncestors(parentOfTopCandidate),
); );
this._someNode(ancestors, function (ancestor) { this._someNode(ancestors, function (ancestor) {
if (!ancestor.tagName) return false; if (!ancestor.tagName) return false;
@ -1582,7 +1582,7 @@ Readability.prototype = {
function (_, hex, numStr) { function (_, hex, numStr) {
var num = parseInt(hex || numStr, hex ? 16 : 10); var num = parseInt(hex || numStr, hex ? 16 : 10);
return String.fromCharCode(num); return String.fromCharCode(num);
} },
); );
}, },
@ -1605,7 +1605,7 @@ Readability.prototype = {
// Strip CDATA markers if present // Strip CDATA markers if present
var content = jsonLdElement.textContent.replace( var content = jsonLdElement.textContent.replace(
/^\s*<!\[CDATA\[|\]\]>\s*$/g, /^\s*<!\[CDATA\[|\]\]>\s*$/g,
"" "",
); );
var parsed = JSON.parse(content); var parsed = JSON.parse(content);
if ( if (
@ -1898,7 +1898,7 @@ Readability.prototype = {
scriptNode.nodeValue = ""; scriptNode.nodeValue = "";
scriptNode.removeAttribute("src"); scriptNode.removeAttribute("src");
return true; return true;
} },
); );
this._removeNodes(this._getAllNodesWithTag(doc, ["noscript"])); this._removeNodes(this._getAllNodesWithTag(doc, ["noscript"]));
}, },
@ -2310,7 +2310,7 @@ Readability.prototype = {
} }
} }
} }
} },
); );
}, },
@ -2323,7 +2323,7 @@ Readability.prototype = {
var children = this._getAllNodesWithTag(e, tags); var children = this._getAllNodesWithTag(e, tags);
this._forEachNode( this._forEachNode(
children, children,
(child) => (childrenLength += this._getInnerText(child, true).length) (child) => (childrenLength += this._getInnerText(child, true).length),
); );
return childrenLength / textLength; return childrenLength / textLength;
}, },
@ -2354,7 +2354,7 @@ Readability.prototype = {
var listNodes = this._getAllNodesWithTag(node, ["ul", "ol"]); var listNodes = this._getAllNodesWithTag(node, ["ul", "ol"]);
this._forEachNode( this._forEachNode(
listNodes, listNodes,
(list) => (listLength += this._getInnerText(list).length) (list) => (listLength += this._getInnerText(list).length),
); );
isList = listLength / this._getInnerText(node).length > 0.9; isList = listLength / this._getInnerText(node).length > 0.9;
} }
@ -2554,7 +2554,7 @@ Readability.prototype = {
var numTags = this._doc.getElementsByTagName("*").length; var numTags = this._doc.getElementsByTagName("*").length;
if (numTags > this._maxElemsToParse) { if (numTags > this._maxElemsToParse) {
throw new Error( throw new Error(
"Aborting parsing document; " + numTags + " elements found" "Aborting parsing document; " + numTags + " elements found",
); );
} }
} }

View File

@ -22,9 +22,11 @@ int shortcut_expand(const char* uri, char* output)
"!blog", "!blog",
"!fnf", "!fnf",
"!fnc", "!fnc",
"!fs",
"!hn", "!hn",
"!hnb" "!hnb",
"!x", "!ww",
"!x"
}; };
char* expansions[] = { char* expansions[] = {
@ -32,9 +34,11 @@ int shortcut_expand(const char* uri, char* output)
"https://nunosempere.com/blog", "https://nunosempere.com/blog",
"https://forum.nunosempere.com/frontpage", "https://forum.nunosempere.com/frontpage",
"https://forum.nunosempere.com/comments", "https://forum.nunosempere.com/comments",
"https://forecasting.substack.com",
"https://news.ycombinator.com", "https://news.ycombinator.com",
"https://news.ycombinator.com/best", "https://news.ycombinator.com/best",
"https://twitter.com", "https://web.whatsapp.com",
"https://twitter.com"
}; };
// len = sizeof(shortcuts) / sizeof(shortcuts[0]); // len = sizeof(shortcuts) / sizeof(shortcuts[0]);

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 7827 + 1000 #define STYLE_N 7831 + 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 7827 + 1000 #define STYLE_N 7831 + 1000
void read_style_js(char* string); void read_style_js(char* string);

View File

@ -2,7 +2,7 @@
// Main part of the code: switch on the domain and select the corresponding style // Main part of the code: switch on the domain and select the corresponding style
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 = `

View File

@ -17,6 +17,7 @@ static struct {
Bar_entry_mode entry_mode; Bar_entry_mode entry_mode;
} bar; } bar;
static int num_tabs = 0; static int num_tabs = 0;
static int custom_style_enabled = 1;
/* Forward declarations */ /* Forward declarations */
void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode); void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode);
@ -71,7 +72,7 @@ void redirect_if_annoying(WebKitWebView* view, const char* uri)
} }
void set_custom_style(WebKitWebView* view) void set_custom_style(WebKitWebView* view)
{ {
if (CUSTOM_STYLE_ENABLED) { if (custom_style_enabled) {
char* style_js = malloc(STYLE_N + 1); char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js); read_style_js(style_js);
webkit_web_view_evaluate_javascript(view, style_js, -1, NULL, "rosenrot-style-plugin", NULL, NULL, NULL); webkit_web_view_evaluate_javascript(view, style_js, -1, NULL, "rosenrot-style-plugin", NULL, NULL, NULL);
@ -251,6 +252,12 @@ int handle_shortcut(func id, GtkNotebook* notebook)
webkit_web_view_go_forward(view); webkit_web_view_go_forward(view);
break; break;
case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */
if (custom_style_enabled)
custom_style_enabled = 0;
else
custom_style_enabled = 1;
// break; passthrough
case refresh: case refresh:
webkit_web_view_reload(view); webkit_web_view_reload(view);
break; break;
@ -302,7 +309,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
} }
break; break;
case toggle_fullscreen: case toggle_fullscreen:
if (is_fullscreen) if (is_fullscreen)
gtk_window_unfullscreen(window); gtk_window_unfullscreen(window);
@ -310,7 +316,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_window_fullscreen(window); gtk_window_fullscreen(window);
is_fullscreen = !is_fullscreen; is_fullscreen = !is_fullscreen;
break; break;
case show_searchbar: case show_searchbar:
toggle_bar(notebook, _SEARCH); toggle_bar(notebook, _SEARCH);
break; break;