Google Docs accessibility refactor (#2023)

* Skip urlRegex if it's used as a filter

* Add getRequiredContentScriptRegistrationPermissions function

* Add a reentrant check to google-docs.js

* Remove script node

* Move forceGoogleDocsHtmlRendering check into google-docs.js

* Replace document-start.js usage with google-docs.js

* Remove documentStart handling

* Add missing parameter descriptions
This commit is contained in:
toasted-nutbread 2021-11-23 22:08:30 -05:00 committed by GitHub
parent ecc994a8bb
commit d454b52a18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 54 deletions

View File

@ -120,7 +120,7 @@
"files": ["ext/**/*.js"], "files": ["ext/**/*.js"],
"excludedFiles": [ "excludedFiles": [
"ext/js/core.js", "ext/js/core.js",
"ext/js/document-start.js", "ext/js/accessibility/google-docs.js",
"ext/js/**/sandbox/**/*.js" "ext/js/**/sandbox/**/*.js"
], ],
"globals": { "globals": {
@ -147,7 +147,7 @@
"files": ["ext/**/*.js"], "files": ["ext/**/*.js"],
"excludedFiles": [ "excludedFiles": [
"ext/js/core.js", "ext/js/core.js",
"ext/js/document-start.js", "ext/js/accessibility/google-docs.js",
"ext/js/yomichan.js", "ext/js/yomichan.js",
"ext/js/**/sandbox/**/*.js" "ext/js/**/sandbox/**/*.js"
], ],

View File

@ -73,7 +73,7 @@
"match_about_blank": true, "match_about_blank": true,
"all_frames": true, "all_frames": true,
"js": [ "js": [
"js/document-start.js" "js/accessibility/google-docs.js"
] ]
} }
], ],

View File

@ -15,7 +15,36 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
(() => { (async () => {
// Reentrant check
if (self.googleDocsAccessibilitySetup) { return; }
self.googleDocsAccessibilitySetup = true;
const invokeApi = (action, params) => {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({action, params}, (response) => {
void chrome.runtime.lastError;
if (typeof response !== 'object' || response === null) {
reject(new Error('Unexpected response'));
} else if (typeof response.error !== 'undefined') {
reject(new Error('Invalid response'));
} else {
resolve(response.result);
}
});
});
};
const optionsContext = {depth: 0, url: location.href};
let options;
try {
options = await invokeApi('optionsGet', {optionsContext});
} catch (e) {
return;
}
if (!options.accessibility.forceGoogleDocsHtmlRendering) { return; }
let parent = document.head; let parent = document.head;
if (parent === null) { if (parent === null) {
parent = document.documentElement; parent = document.documentElement;
@ -24,4 +53,5 @@
const script = document.createElement('script'); const script = document.createElement('script');
script.textContent = 'window._docs_force_html_by_ext = true;'; script.textContent = 'window._docs_force_html_by_ext = true;';
parent.appendChild(script); parent.appendChild(script);
parent.removeChild(script);
})(); })();

View File

@ -127,7 +127,6 @@ class Backend {
['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}], ['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}],
['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}], ['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}],
['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}], ['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}],
['documentStart', {async: false, contentScript: true, handler: this._onApiDocumentStart.bind(this)}],
['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}] ['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
@ -747,12 +746,6 @@ class Backend {
return this._japaneseUtil.isStringPartiallyJapanese(text); return this._japaneseUtil.isStringPartiallyJapanese(text);
} }
_onApiDocumentStart(params, sender) {
const {tab, frameId, url} = sender;
if (typeof url !== 'string' || typeof tab !== 'object' || tab === null) { return; }
this._updateTabAccessibility(url, tab.id, frameId);
}
async _onApiGetTermFrequencies({termReadingList, dictionaries}) { async _onApiGetTermFrequencies({termReadingList, dictionaries}) {
return await this._translator.getTermFrequencies(termReadingList, dictionaries); return await this._translator.getTermFrequencies(termReadingList, dictionaries);
} }
@ -2140,25 +2133,6 @@ class Backend {
} }
} }
async _updateTabAccessibility(url, tabId, frameId) {
let file = null;
switch (new URL(url).hostname) {
case 'docs.google.com':
{
const optionsContext = {depth: 0, url};
const options = this._getProfileOptions(optionsContext);
if (!options.accessibility.forceGoogleDocsHtmlRendering) { return; }
file = 'js/accessibility/google-docs.js';
}
break;
}
if (file === null) { return; }
await this._scriptManager.injectScript(file, tabId, frameId, false, true, 'document_start');
}
async _getNormalizedDictionaryDatabaseMedia(targets) { async _getNormalizedDictionaryDatabaseMedia(targets) {
const results = await this._dictionaryDatabase.getMedia(targets); const results = await this._dictionaryDatabase.getMedia(targets);
for (const item of results) { for (const item of results) {

View File

@ -106,9 +106,9 @@ class ScriptManager {
* @param {string} id A unique identifier for the registration. * @param {string} id A unique identifier for the registration.
* @param {object} details The script registration details. * @param {object} details The script registration details.
* @param {boolean} [details.allFrames] Same as `all_frames` in the `content_scripts` manifest key. * @param {boolean} [details.allFrames] Same as `all_frames` in the `content_scripts` manifest key.
* @param {string[]} [details.css] * @param {string[]} [details.css] List of CSS paths.
* @param {string[]} [details.excludeMatches] Same as `exclude_matches` in the `content_scripts` manifest key. * @param {string[]} [details.excludeMatches] Same as `exclude_matches` in the `content_scripts` manifest key.
* @param {string[]} [details.js] * @param {string[]} [details.js] List of script paths.
* @param {boolean} [details.matchAboutBlank] Same as `match_about_blank` in the `content_scripts` manifest key. * @param {boolean} [details.matchAboutBlank] Same as `match_about_blank` in the `content_scripts` manifest key.
* @param {string[]} details.matches Same as `matches` in the `content_scripts` manifest key. * @param {string[]} details.matches Same as `matches` in the `content_scripts` manifest key.
* @param {string} [details.urlMatches] Regex match pattern to use as a fallback * @param {string} [details.urlMatches] Regex match pattern to use as a fallback
@ -181,6 +181,31 @@ class ScriptManager {
return true; return true;
} }
/**
* Gets the optional permissions required to register a content script.
* @returns {string[]} An array of the required permissions, which may be empty.
*/
getRequiredContentScriptRegistrationPermissions() {
if (
// Firefox
(
typeof browser === 'object' && browser !== null &&
isObject(browser.contentScripts) &&
typeof browser.contentScripts.register === 'function'
) ||
// Chrome
(
isObject(chrome.scripting) &&
typeof chrome.scripting.registerContentScripts === 'function'
)
) {
return [];
}
// Fallback
return ['webNavigation'];
}
// Private // Private
_injectStylesheetMV2(type, content, tabId, frameId, allFrames, matchAboutBlank, runAt) { _injectStylesheetMV2(type, content, tabId, frameId, allFrames, matchAboutBlank, runAt) {
@ -333,8 +358,7 @@ class ScriptManager {
_registerContentScriptFallback(id, details) { _registerContentScriptFallback(id, details) {
const {allFrames, css, js, matchAboutBlank, runAt, urlMatches} = details; const {allFrames, css, js, matchAboutBlank, runAt, urlMatches} = details;
const urlRegex = new RegExp(urlMatches); const details2 = {allFrames, css, js, matchAboutBlank, runAt, urlRegex: null};
const details2 = {allFrames, css, js, matchAboutBlank, runAt, urlRegex};
let unregister; let unregister;
const webNavigationEvent = this._getWebNavigationEvent(runAt); const webNavigationEvent = this._getWebNavigationEvent(runAt);
if (isObject(webNavigationEvent)) { if (isObject(webNavigationEvent)) {
@ -356,6 +380,7 @@ class ScriptManager {
chrome.tabs.onUpdated.addListener(onTabUpdated, extraParameters); chrome.tabs.onUpdated.addListener(onTabUpdated, extraParameters);
} catch (e) { } catch (e) {
// Chrome // Chrome
details2.urlRegex = new RegExp(urlMatches);
chrome.tabs.onUpdated.addListener(onTabUpdated); chrome.tabs.onUpdated.addListener(onTabUpdated);
} }
unregister = () => chrome.tabs.onUpdated.removeListener(onTabUpdated); unregister = () => chrome.tabs.onUpdated.removeListener(onTabUpdated);
@ -378,7 +403,7 @@ class ScriptManager {
async _injectContentScript(isWebNavigation, details, status, url, tabId, frameId) { async _injectContentScript(isWebNavigation, details, status, url, tabId, frameId) {
const {urlRegex} = details; const {urlRegex} = details;
if (typeof urlRegex !== 'undefined' && !urlRegex.test(url)) { return; } if (urlRegex !== null && !urlRegex.test(url)) { return; }
let {allFrames, css, js, matchAboutBlank, runAt} = details; let {allFrames, css, js, matchAboutBlank, runAt} = details;

View File

@ -1,18 +0,0 @@
/*
* Copyright (C) 2021 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
chrome.runtime.sendMessage({action: 'documentStart'}, () => void chrome.runtime.lastError);

View File

@ -72,7 +72,7 @@
"match_about_blank": true, "match_about_blank": true,
"all_frames": true, "all_frames": true,
"js": [ "js": [
"js/document-start.js" "js/accessibility/google-docs.js"
] ]
} }
], ],