Merge pull request #4748 from natrys/reader

Provide hook for basic CSS override for new Reader mode
This commit is contained in:
Oliver Blanthorn 2023-08-23 20:47:25 +00:00 committed by GitHub
commit 19a29c1d9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 18 deletions

View file

@ -6074,6 +6074,17 @@ export async function readerurl() {
* Use `:reader --old` to use Firefox's built-in reader mode, which Tridactyl can't run on. * Use `:reader --old` to use Firefox's built-in reader mode, which Tridactyl can't run on.
* *
* __NB:__ the reader page is a privileged environment which has access to all Tridactyl functions, notably the native messenger if you have it installed. We are parsing untrusted web-content to run in this environment. Mozilla's readability library will strip out most of these, then we use a sanitation library, `js-xss`, to strip out any remaining unsafe tags, but if there was a serious bug in this library, and a targeted attack against Tridactyl, an attacker could get remote code execution. If you're worried about this, use `:reader --old` instead or only use `:reader` on pages you trust. * __NB:__ the reader page is a privileged environment which has access to all Tridactyl functions, notably the native messenger if you have it installed. We are parsing untrusted web-content to run in this environment. Mozilla's readability library will strip out most of these, then we use a sanitation library, `js-xss`, to strip out any remaining unsafe tags, but if there was a serious bug in this library, and a targeted attack against Tridactyl, an attacker could get remote code execution. If you're worried about this, use `:reader --old` instead or only use `:reader` on pages you trust.
*
* You may use [userContent.css](http://kb.mozillazine.org/index.php?title=UserContent.css&printable=yes) to enhance or override default styling of the new reader view. The `body` of the page has id `tridactyl-reader` and the article content follows in a `main` tag. Therefore to alter default styling, you can do something like this in your `userContent.css`:
*
* ```css
* #tridactyl-reader > main {
* width: 80vw !important;
* text-align: left;
* }
* ```
*
* Follow [issue #4657](https://github.com/tridactyl/tridactyl/issues/4657) if you would like to find out when we have made a more user-friendly solution.
*/ */
//#content //#content
export async function reader(...args: string[]) { export async function reader(...args: string[]) {

View file

@ -1290,6 +1290,11 @@ export class default_config {
* https://fusejs.io/api/options.html#threshold * https://fusejs.io/api/options.html#threshold
*/ */
completionfuzziness = 0.3 completionfuzziness = 0.3
/**
* Whether to show article url in the document.title of Reader View.
*/
readerurlintitle: "true" | "false" = "false"
} }
const platform_defaults = { const platform_defaults = {

View file

@ -1,14 +1,30 @@
// import * as config from "@src/lib/config" import * as config from "@src/lib/config"
import xss from "xss" import xss from "xss"
function updatePage(){ async function updatePage() {
const article = JSON.parse(decodeURIComponent(atob(window.location.hash.substr(1)))) const article = JSON.parse(
article.content = xss(article.content, {stripIgnoreTag: true}) decodeURIComponent(atob(window.location.hash.substr(1))),
document.body.innerHTML = article.content )
article.content = xss(article.content, { stripIgnoreTag: true })
const content = document.createElement("main")
content.innerHTML = article.content
document.body.appendChild(content)
if (article.title !== undefined) { if (article.title !== undefined) {
const header = document.createElement("header") const header = document.createElement("header")
const title = document.createElement("h1") const title = document.createElement("h1")
document.title = [article.siteName, article.title].filter(Boolean).join(": ") // sensible? if (
(await config.getAsync("readerurlintitle")) == "true" &&
!(article.title ?? "").includes(article.link)
) {
document.title =
[article.siteName, article.title].filter(Boolean).join(": ") +
" :: " +
article.link
} else {
document.title = [article.siteName, article.title]
.filter(Boolean)
.join(": ")
}
title.textContent = article.title title.textContent = article.title
header.appendChild(title) header.appendChild(title)
if (article.byline !== undefined) { if (article.byline !== undefined) {
@ -19,7 +35,9 @@ function updatePage(){
document.body.insertBefore(header, document.body.firstChild) document.body.insertBefore(header, document.body.firstChild)
} }
if (article.link !== undefined) { if (article.link !== undefined) {
const link = (document.getElementById("tricanonlink") as HTMLLinkElement) ?? document.createElement("link") const link =
(document.getElementById("tricanonlink") as HTMLLinkElement) ??
document.createElement("link")
link.rel = "canonical" link.rel = "canonical"
link.id = "tricanonlink" link.id = "tricanonlink"
link.href = article.link link.href = article.link

View file

@ -91,29 +91,22 @@ html {
scroll-behavior: smooth; scroll-behavior: smooth;
} }
/* Make the body a nice central block */
body { body {
color: var(--text); color: var(--text);
background-color: var(--bg); background-color: var(--bg);
font-size: 1.15rem; font-size: 1.15rem;
line-height: 1.5; line-height: 1.5;
display: grid;
grid-template-columns: 1fr min(45rem, 90%) 1fr;
margin: 0; margin: 0;
text-align: justify; text-align: justify;
hyphens: auto; hyphens: auto;
} }
body > * {
grid-column: 2;
}
/* Make the header bg full width, but the content inline with body */ /* Make the header bg full width, but the content inline with body */
body > header { body > header {
background-color: var(--accent-bg); background-color: var(--accent-bg);
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
text-align: center; text-align: center;
padding: 0 0.5rem 2rem 0.5rem; padding: 2rem 0.5rem 2rem 0.5rem;
grid-column: 1 / -1;
} }
body > header h1 { body > header h1 {
@ -126,9 +119,10 @@ body > header p {
margin: 1rem auto; margin: 1rem auto;
} }
/* Add a little padding to ensure spacing is correct between content and header > nav */ body > main {
main {
padding-top: 1.5rem; padding-top: 1.5rem;
width: min(45rem, 90%);
margin: auto;
} }
body > footer { body > footer {

View file

@ -8,7 +8,7 @@
<link rel="stylesheet" href="css/hint.css"> <link rel="stylesheet" href="css/hint.css">
<link rel="stylesheet" href="css/viewsource.css"> <link rel="stylesheet" href="css/viewsource.css">
</head> </head>
<body> <body id="tridactyl-reader">
</body> </body>
<script src="../content.js"></script> <script src="../content.js"></script>
<script src="../reader.js"></script> <script src="../reader.js"></script>