Skip to content

Commit

Permalink
Automatic wordcount tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
fjwillemsen committed Aug 12, 2022
1 parent daeb6ba commit ef199c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ However, would it not be even better if it were to behave like a native app on y
## Tips
- The preferences pane can be found in the same left-side menu as the other Overleaf settings.
- You can run multiple instances of Native Overleaf, allowing you to keep multiple projects open at the same time and receive notifications for each project.
- The word count tracker tracks the total words in the project, so includes anything written by other authors. It counts the words in the compiled PDF, hence it must be recompiled to reflect recent changes.
- The word count tracker tracks the total words in the project, so includes anything written by other authors. It counts the words in the compiled PDF, hence it must be recompiled to track a new change in the number of words.
- The word count tracker uses the local date of your system, hence inconsistencies may arise when switching time zones. If necessary, the word count can be reset using the button in the preferences pane.
- Notice on notifications: For notifications to work, the app must be allowed to by your system. You will only receive notifications for projects that are opened in the background, so you will not get notifications while working in a project. **Important**: to get notifications for chats, the chat window must have been opened at least once after loading a project (you can close it again). This is a limitation of the way we listen for new chat messages. If someone has a better idea, please get in touch.

Expand Down
1 change: 1 addition & 0 deletions Scripts/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function setupPreferencesPane() {
</select>
<hr/>
<h6>Wordcount Tracking</h6>
<p>When the document is recompiled, this keeps track of the number of words, allowing you to see your progress.</p>
<label class="settings-toggle">
<input id="wordcount_tracking" class="settings-toggle-checkbox" type="checkbox">
<div class="settings-toggle-switch"></div>
Expand Down
31 changes: 17 additions & 14 deletions Scripts/wordcount.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ async function getWordCount() {
if (wordcount_el && wordcount_el !== undefined && wordcount_el.scope !== undefined) {
let wordcount_scope = wordcount_el.scope();

if (wordcount_scope !== undefined && (await waitUntilPDFCompiled()) == true) {
if (wordcount_scope !== undefined && (await waitUntilPDFCompiled()) != false) {
console.log("gwc 1");
wordcount_scope.openWordCountModal();
console.log("gwc 2");

// check if the wordcount is loaded in quick successions, 100 attempts with a timeout of 50ms
const wordcount = await recursiveCheckAndWait(extractWordCount, 50, 100);
Expand All @@ -44,6 +46,7 @@ async function getWordCount() {
return wordcount;
}
}
return;
}

// retrieve the wordcounts from local storage and add the keys where necessary
Expand Down Expand Up @@ -87,7 +90,6 @@ async function updateWordCount() {
// update the latest wordcount
wordcounts[this.project_id][currentdate].latest = wordcount;
const achieved_wordcount = wordcount - wordcounts[this.project_id][currentdate].earliest;

// notify the user if the target number of words are reached
if (hasbeennotified == false && up_wordcount_dailytarget > 0 && achieved_wordcount >= up_wordcount_dailytarget) {
new Notification("Awesome, already met today's target!", {
Expand Down Expand Up @@ -142,21 +144,22 @@ async function setupWordCount() {
return;
}

// set the initial word count on load
await waitUntilPDFCompiled();
updateWordCount();
if ((await waitUntilPDFCompiled()) != false) {
// set the initial word count on load
updateWordCount();

// set the observer for when the PDF is recompiled
let pdf_element = getBackupElement(1);
if (compilation_observer === undefined) {
compilation_observer = new MutationObserver(function (mutations) {
console.log("PDF compiled, updating wordcount");
updateWordCount();
// set the observer for when the PDF is recompiled
let pdf_element = getBackupElement(1);
if (compilation_observer === undefined) {
compilation_observer = new MutationObserver(function (mutations) {
console.log("PDF compiled, updating wordcount");
updateWordCount();
});
}
compilation_observer.observe(pdf_element, {
attributes: true,
});
}
compilation_observer.observe(pdf_element, {
attributes: true,
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion bundled_script.js

Large diffs are not rendered by default.

0 comments on commit ef199c4

Please sign in to comment.