Skip to content

Commit

Permalink
Add reader mode for simplifying pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauve Signweaver committed Aug 29, 2023
1 parent c0b3211 commit 56fc68b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ bundle-gemini.js
bundle-json.js
bundle-markdown.js
bundle-ssb.js
bundle-reader.js
13 changes: 13 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ if(knownType) {

console.log('Waiting to inject rendering code', { SCRIPT_MAP, TYPE_MAP })

chrome.browserAction.onClicked.addListener(({ id: tabId }) => {
const readerCode = `
(function (){
const src = "${scriptURL('reader')}"
const script = document.createElement('script')
script.setAttribute('src', src)
script.setAttribute('charset', 'utf-8')
document.body.appendChild(script)
})();
`
chrome.tabs.executeScript(tabId, { code: readerCode })
})

function handleLoaded ({ tabId, url }) {
const { protocol } = new URL(url)
if (FORBIDDEN_PROTOCOLS.includes(protocol)) return
Expand Down
10 changes: 9 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@
"scripts": ["background.js"]
},

"browser_action": {
"default_icon": {
"32": "reader.png"
},
"default_title": "Reader View"
},

"web_accessible_resources": [
"bundle-gemini.js",
"bundle-markdown.js",
"bundle-json.js",
"bundle-ssb.js"
"bundle-ssb.js",
"bundle-reader.js"
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build-json": "browserify ./json.js > bundle-json.js",
"build-gemini": "browserify ./gemini.js > bundle-gemini.js",
"build-ssb": "browserify ./ssb.js > bundle-ssb.js",
"build": "npm run build-markdown && npm run build-json && npm run build-gemini && npm run build-ssb"
"build-reader": "browserify ./reader.js > bundle-reader.js",
"build": "npm run build-markdown && npm run build-json && npm run build-gemini && npm run build-ssb && npm run build-reader"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +28,7 @@
"standard": "^17.0.0"
},
"dependencies": {
"@mozilla/readability": "^0.4.4",
"escape-html": "^1.0.3",
"gemini-to-html": "^2.1.0",
"html-to-text": "^8.2.0",
Expand Down
20 changes: 20 additions & 0 deletions reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const renderPage = require('./template.js')
const {Readability} = require('@mozilla/readability')

const article = new Readability(document).parse()

const { title, content, byline } = article

const finalContent = `
<h1>${title}</h1>
<p>
${byline}
</p>
<hr>
${content}
`

renderPage(finalContent, title)
Binary file added reader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions reader.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 56fc68b

Please sign in to comment.