Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions extension/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,25 @@ chrome.webRequest.onBeforeRequest.addListener(
// https://stackoverflow.com/questions/10994324/chrome-extension-content-script-re-injection-after-upgrade-or-install

chrome.runtime.onInstalled.addListener(async () => {
for (const cs of chrome.runtime.getManifest().content_scripts) {
for (const tab of await chrome.tabs.query({url: cs.matches})) {
for (const contentScript of chrome.runtime.getManifest().content_scripts) {
for (const tab of await chrome.tabs.query({url: contentScript.matches})) {
// Unload the dead content script by removing its code from the page
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: cs.js,
target: { tabId: tab.id, allFrames: true },
func: function() {
var scripts = document.getElementsByTagName('script');
for(var i = scripts.length - 1; i >= 0; i--) {
if(scripts[i].src === `chrome-extension://${chrome.runtime.id}/writing.js`) {
scripts[i].remove();
}
}
}
});

// re-inject content script
chrome.scripting.executeScript({
target: {tabId: tab.id, allFrames: true},
files: contentScript.js,
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions extension/extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
}
},
"content_scripts": [{
"matches": ["*://docs.google.com/*"],
"matches": ["*://docs.google.com/document/*"],
"js": ["3rdparty/sha256.js", "writing_common.js", "writing.js"]
}],
"web_accessible_resources": [{
"web_accessible_resources": [{
"resources": ["inject.js"],
"matches": ["*://docs.google.com/*"],
"use_dynamic_url": true
Expand All @@ -37,7 +37,7 @@
"activeTab"
],
"host_permissions": [
"*://docs.google.com/*"
"*://docs.google.com/document/*"
],
"icons": {
"48": "icons/lousy-fountain-pen-48.png"
Expand Down
15 changes: 12 additions & 3 deletions extension/extension/writing.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ function log_event(event_type, event) {
// uncomment to watch events being logged from the client side with devtools
// console.log(event);

if (chrome.runtime.id !== undefined) {
// Check if the extension runtime still has its context
if (chrome.runtime?.id !== undefined) {
chrome.runtime.sendMessage(event);
}
}
Expand Down Expand Up @@ -97,7 +98,7 @@ function google_docs_partial_text() {
reconstructing text.
*/
try {
return document.getElementsByClassName("kix")[0].innerText;
return document.getElementsByClassName("kix-page")[0].innerText;
} catch(error) {
log_error("Could not get document text");
return null;
Expand Down Expand Up @@ -699,7 +700,15 @@ var MUTATION_OBSERVER_OPTIONS = {
// OK, now create the MutationObserver and start running it
// on the document.
observer = prepare_mutation_observer();
observer.observe(editor, MUTATION_OBSERVER_OPTIONS);
chrome.runtime.onMessage.addListener(function(message) {
if (message === 'startObserving') {
// Start observing the target node for configured mutations
observer.observe(editor, MUTATION_OBSERVER_OPTIONS);
} else if (message === 'stopObserving') {
// Stop observing the target node
observer.disconnect();
}
});

/*
Document Load Code Block
Expand Down