Last active
January 13, 2024 16:32
-
-
Save zachthedev/0df884f490ec4a171ff0a8f8403a7926 to your computer and use it in GitHub Desktop.
Brings back visual last edit text in drive apps due to M3 migration by Google.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Restore Revision Time Visual Text in Google Apps | |
// @version 1.2 | |
// @description Brings back visual last edit text in drive apps due to M3 migration by Google. | |
// @author ZachTheDev | |
// @match https://docs.google.com/document* | |
// @match https://docs.google.com/presentation* | |
// @match https://docs.google.com/spreadsheets* | |
// ==/UserScript== | |
(function() { | |
function addBackRevisionVisualText() { | |
const escapeHTMLPolicy = trustedTypes.createPolicy("use-raw-string", { | |
createHTML: (string) => string, | |
}); | |
const revisionButtonElement = document.getElementById("docs-revisions-appbarbutton"); | |
var revisionTextFromButton = revisionButtonElement.getAttribute("data-tooltip"); | |
const menubarElement = document.getElementById("docs-menubar"); | |
const revisionVisualTextHTML = "<div id=\"revisionVisualText\" class=\"menu-button goog-control goog-inline-block\" role=\"menuitem\" style=\"background-color: transparent;text-decoration: underline;\" data-tooltip=\"Open version history\"></div>"; | |
const rangeForRevisionVisualTextHTML = document.createRange(); | |
const fragmentForRevisionVisualTextHTML = rangeForRevisionVisualTextHTML.createContextualFragment(escapeHTMLPolicy.createHTML(revisionVisualTextHTML)); | |
menubarElement.appendChild(fragmentForRevisionVisualTextHTML); | |
const revisionVisualTextElement = document.getElementById("revisionVisualText"); | |
const setAttributeWatcher = revisionButtonElement.setAttribute; | |
revisionButtonElement.setAttribute = (key, value) => { | |
revisionTextFromButton = revisionButtonElement.getAttribute("data-tooltip"); | |
revisionVisualTextElement.innerHTML = escapeHTMLPolicy.createHTML(revisionTextFromButton); | |
setAttributeWatcher.call(revisionButtonElement, key, value); | |
}; | |
revisionVisualTextElement.addEventListener("mousedown", function (event) { | |
revisionButtonElement.dispatchEvent(new MouseEvent("mousedown")); | |
revisionButtonElement.classList.remove("jfk-button-hover"); | |
revisionButtonElement.dispatchEvent(new MouseEvent("mouseup")); | |
event.stopPropagation(); // fixes bug where menus would open on hover after first click of the revisionVisualTextElement | |
}); | |
revisionButtonElement.addEventListener("mouseenter", (event) => {revisionButtonElement.classList.add("jfk-button-hover");}, false); | |
} | |
addBackRevisionVisualText(); | |
})(); |
@andersonaddo Good catch about not naming it "default", I was wondering why a policy name had to be provided when creating a policy
@andersonaddo Thanks for catching that. I have updated my version to reflect this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tech234a thanks for the heads up, I changed my script as well. I had to make mine more type safe since trustedTypes exists on Chrome but not Firefox. Also, we shouldn't name this policy "default", since it will be used everywhere (not sure if its everywhere in the script or in the entire document). Reference: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory/createPolicy
cc @zachthedev