Skip to content

Commit

Permalink
fix paging issue in Pageless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ken107 committed Nov 29, 2023
1 parent 040c38b commit 21d216c
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions js/content/google-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ function SvgReadAloudDoc() {
if (currentIndex == -1) return null
var nextIndex = currentIndex + (nextPageNumber - currentPageNumber)

// function to remove overlap between pages (in Pageless mode)
const overlapRemover = nextPageNumber == currentPageNumber +1
? makeOverlapRemover(pages[currentIndex])
: () => true;

// if the next page is not loaded and is an earlier page
if (nextIndex < head) {
pages[head].scrollIntoView(); await waitMillis(500)
Expand Down Expand Up @@ -379,11 +384,12 @@ function SvgReadAloudDoc() {
// scroll into view and return text
if (!quietly) currentPage.scrollIntoView()
return $("svg > g[role=paragraph]", currentPage).get()
.map(para => {
.flatMap(para => {
return $(para).children("rect").get()
.map(el => el.getAttribute("aria-label"))
.filter(overlapRemover)
.filter(makeDeduper())
.join(" ")
.join(" ") || []
})
}

Expand All @@ -406,7 +412,7 @@ function SvgReadAloudDoc() {
return $(".kix-page-paginated").get();
} else if($(".kix-rotatingtilemanager-content").length) {
console.log("Pageless google doc detected.");
return $(".kix-rotatingtilemanager-content").get();
return $(".kix-rotatingtilemanager-content").children().get();
} else {
console.log("Could not detect paginated or pageless google doc.");
}
Expand Down Expand Up @@ -444,4 +450,24 @@ function SvgReadAloudDoc() {
return true
}
}

function makeOverlapRemover(prevPage) {
const prevPageTexts = Array.from(prevPage.querySelectorAll("svg > g[role=paragraph] > rect"))
.map(rect => rect.getAttribute("aria-label"))
var indexOfLastMatch = null
return function(text) {
if (indexOfLastMatch == null) {
//find index of the start of the overlapping section
indexOfLastMatch = prevPageTexts.lastIndexOf(text)
if (indexOfLastMatch != -1) console.debug("Overlap detected", prevPageTexts.length-indexOfLastMatch)
}
else if (indexOfLastMatch > 0) {
//if subsequent lines match, keep incrementing index
if (prevPageTexts[indexOfLastMatch +1] == text) indexOfLastMatch += 1
else indexOfLastMatch = -1
}
//return false to filter out matches
return indexOfLastMatch > 0 ? false : true
}
}
}

0 comments on commit 21d216c

Please sign in to comment.