Skip to content

Commit

Permalink
Add reader mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolascolla committed Oct 13, 2021
1 parent 9b7c5ff commit 66ff7a1
Show file tree
Hide file tree
Showing 5 changed files with 2,475 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ build/
*.qmlc
*.jsc
.directory

*.diff
*.rej
*.orig
30 changes: 29 additions & 1 deletion src/app/webbrowser/AddressBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ FocusScope {
property var findController: null
property color fgColor: theme.palette.normal.baseText

property bool isReaderable: false
property bool readerMode: false
signal toggleReaderMode()
property var certificateErrorsMap: null
property bool lastLoadSucceeded
readonly property bool hasSecurityError: (actualScheme === "https") && (certificateErrorsMap[UrlUtils.extractHost(actualUrl)] !== undefined)
Expand Down Expand Up @@ -207,7 +210,32 @@ FocusScope {

secondaryItem: Row {
height: textField.height
id: secondaryIconsRow
MouseArea {
id: readerToggle
objectName: "readerToggle"

height: parent.height
width: visible ? height : 0

visible: bookmarkToggle.visible && isReaderable

Icon {
height: parent.height - units.gu(2)
width: height
anchors.centerIn: parent

name: "stock_ebook"
color: readerMode ? theme.palette.normal.focus : addressbar.fgColor
}

onClicked: addressbar.toggleReaderMode()

Item {
id: readerTogglePlaceHolderItem
anchors.fill: parent
}
}
MouseArea {
id: bookmarkToggle
objectName: "bookmarkToggle"
Expand Down Expand Up @@ -273,7 +301,7 @@ FocusScope {
anchors {
fill: parent
leftMargin: icons.width
rightMargin: bookmarkToggle.width
rightMargin: secondaryIconsRow.width
}

enabled: !addressbar.activeFocus
Expand Down
10 changes: 9 additions & 1 deletion src/app/webbrowser/NavigationBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ FocusScope {
property alias searchUrl: addressbar.searchUrl
readonly property string text: addressbar.text
property alias bookmarked: addressbar.bookmarked


signal closeTabRequested()
signal toggleBookmark()
signal toggleDownloads()
Expand Down Expand Up @@ -166,7 +168,8 @@ FocusScope {
fgColor: root.fgColor

focus: true

isReaderable: tab && tab.isReaderable
readerMode: tab && tab.readerMode
findInPageMode: findInPageMode
findController: internal.webview ? internal.webview.findController : null
certificateErrorsMap: internal.webview ? internal.webview.certificateErrorsMap : ({})
Expand Down Expand Up @@ -197,11 +200,16 @@ FocusScope {
}
onRequestStop: internal.webview.stop()
onToggleBookmark: root.toggleBookmark()
onToggleReaderMode: {
tab.toggleReaderMode()

}

Connections {
target: tab
onUrlChanged: addressbar.actualUrl = tab.url
}

}

Row {
Expand Down
27 changes: 27 additions & 0 deletions src/app/webbrowser/TabComponent.qml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Component {
property var internal
property var recentView
property var tabsModel
property bool readerMode: false
property bool isReaderable: false

Item {
id: contextualMenuTarget
Expand All @@ -71,6 +73,15 @@ Component {
focus: true

enabled: current && !bottomEdgeHandle.dragging && !recentView.visible && parent.focus
userScripts: [
WebEngineScript {
name: "readability"
injectionPoint: WebEngineScript.DocumentReady
sourceUrl: Qt.resolvedUrl("readability.js")
runOnSubframes: true
worldId: WebEngineScript.MainWorld
}
]

onNewViewRequested: function(request) {

Expand Down Expand Up @@ -143,6 +154,12 @@ Component {
chrome.findInPageMode = false
webviewInternal.titleSet = false
webviewInternal.title = title
readerMode = false

webviewimpl.runJavaScript("isProbablyReaderable(document)", function(res) {
isReaderable = res
console.log('readerable', res)
});
}

if (webviewimpl.incognito) {
Expand Down Expand Up @@ -279,5 +296,15 @@ Component {
}
}
}
function toggleReaderMode() {
if (browserTab.readerMode) {
browserTab.reload()
return
}

browser.currentWebview.runJavaScript("makeDomReadable()", function(r) {
browserTab.readerMode = true
})
}
}
}
Loading

0 comments on commit 66ff7a1

Please sign in to comment.