DOMParser replacement (#561)
* Add script to build library files * Add built parse5 library * Add new SimpleDOMParser which uses parse5 * Update license info * Update MV3 build to use the new SimpleDOMParser * Update file exclusions * Hide/clarify license info for MV2 builds
This commit is contained in:
parent
9beb659b17
commit
05d4049f16
@ -179,7 +179,7 @@
|
||||
"ext/bg/js/options.js",
|
||||
"ext/bg/js/profile-conditions.js",
|
||||
"ext/bg/js/request-builder.js",
|
||||
"ext/bg/js/native-simple-dom-parser.js",
|
||||
"ext/bg/js/simple-dom-parser.js",
|
||||
"ext/bg/js/text-source-map.js",
|
||||
"ext/bg/js/translator.js",
|
||||
"ext/bg/js/backend.js",
|
||||
|
@ -266,6 +266,7 @@ versions packaged.
|
||||
* jQuery: [homepage](https://blog.jquery.com/) - [snapshot](https://code.jquery.com/jquery-3.2.1.min.js) - [license](https://github.com/jquery/jquery/blob/3.2.1/LICENSE.txt)
|
||||
* JSZip: [homepage](https://stuk.github.io/jszip/) - [snapshot](https://raw.githubusercontent.com/Stuk/jszip/de7f52fbcba485737bef7923a83f0fad92d9f5bc/dist/jszip.min.js) - [license](https://github.com/Stuk/jszip/blob/v3.1.3/LICENSE.markdown)
|
||||
* WanaKana: [homepage](https://wanakana.com/) - [snapshot](https://unpkg.com/wanakana@4.0.2/umd/wanakana.min.js) - [license](https://github.com/WaniKani/WanaKana/blob/4.0.2/LICENSE)
|
||||
* parse5: [homepage](https://github.com/inikulin/parse5) - [snapshot](https://github.com/inikulin/parse5/tree/v6.0.1/packages/parse5) - [license](https://github.com/inikulin/parse5/blob/v6.0.1/LICENSE) _(Only used in MV3 build)_
|
||||
|
||||
## Frequently Asked Questions ##
|
||||
|
||||
|
39
dev/build-libs.js
Normal file
39
dev/build-libs.js
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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/>.
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const browserify = require('browserify');
|
||||
|
||||
async function main() {
|
||||
const extLibPath = path.join(__dirname, '..', 'ext', 'mixed', 'lib');
|
||||
const parse5Path = require.resolve('parse5');
|
||||
|
||||
const content = await new Promise((resolve, reject) => {
|
||||
browserify([parse5Path], {standalone: 'parse5', debug: true}).bundle((error, result) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
fs.writeFileSync(path.join(extLibPath, 'parse5.js'), content);
|
||||
}
|
||||
|
||||
main();
|
@ -110,7 +110,9 @@
|
||||
"name": "chrome",
|
||||
"fileName": "yomichan-chrome.zip",
|
||||
"excludeFiles": [
|
||||
"sw.js"
|
||||
"sw.js",
|
||||
"bg/js/simple-dom-parser.js",
|
||||
"mixed/lib/parse5.js"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -133,7 +135,9 @@
|
||||
}
|
||||
],
|
||||
"excludeFiles": [
|
||||
"sw.js"
|
||||
"sw.js",
|
||||
"bg/js/simple-dom-parser.js",
|
||||
"mixed/lib/parse5.js"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -155,6 +159,10 @@
|
||||
{"action": "move", "path": ["web_accessible_resources"], "newPath": ["web_accessible_resources_old"]},
|
||||
{"action": "set", "path": ["web_accessible_resources"], "value": [{"resources": [], "matches": ["<all_urls>"]}], "after": "web_accessible_resources_old"},
|
||||
{"action": "move", "path": ["web_accessible_resources_old"], "newPath": ["web_accessible_resources", 0, "resources"]}
|
||||
],
|
||||
"excludeFiles": [
|
||||
"bg/background.html",
|
||||
"bg/js/native-simple-dom-parser.js"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -187,7 +195,9 @@
|
||||
}
|
||||
],
|
||||
"excludeFiles": [
|
||||
"sw.js"
|
||||
"sw.js",
|
||||
"bg/js/simple-dom-parser.js",
|
||||
"mixed/lib/parse5.js"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -216,7 +226,9 @@
|
||||
}
|
||||
],
|
||||
"excludeFiles": [
|
||||
"sw.js"
|
||||
"sw.js",
|
||||
"bg/js/simple-dom-parser.js",
|
||||
"mixed/lib/parse5.js"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
/* global
|
||||
* NativeSimpleDOMParser
|
||||
* SimpleDOMParser
|
||||
*/
|
||||
|
||||
class AudioDownloader {
|
||||
@ -239,8 +240,10 @@ class AudioDownloader {
|
||||
}
|
||||
|
||||
_createSimpleDOMParser(content) {
|
||||
if (NativeSimpleDOMParser.isSupported()) {
|
||||
if (typeof NativeSimpleDOMParser !== 'undefined' && NativeSimpleDOMParser.isSupported()) {
|
||||
return new NativeSimpleDOMParser(content);
|
||||
} else if (typeof SimpleDOMParser !== 'undefined' && SimpleDOMParser.isSupported()) {
|
||||
return new SimpleDOMParser(content);
|
||||
} else {
|
||||
throw new Error('DOM parsing not supported');
|
||||
}
|
||||
|
@ -17,8 +17,6 @@
|
||||
|
||||
class NativeSimpleDOMParser {
|
||||
constructor(content) {
|
||||
// TODO : Remove
|
||||
// eslint-disable-next-line no-undef
|
||||
this._document = new DOMParser().parseFromString(content, 'text/html');
|
||||
}
|
||||
|
||||
|
117
ext/bg/js/simple-dom-parser.js
Normal file
117
ext/bg/js/simple-dom-parser.js
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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/>.
|
||||
*/
|
||||
|
||||
/* globals
|
||||
* parse5
|
||||
*/
|
||||
|
||||
class SimpleDOMParser {
|
||||
constructor(content) {
|
||||
this._document = parse5.parse(content);
|
||||
}
|
||||
|
||||
getElementById(id, root=null) {
|
||||
for (const node of this._allNodes(root)) {
|
||||
if (typeof node.tagName === 'string' && this.getAttribute(node, 'id') === id) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getElementByTagName(tagName, root=null) {
|
||||
for (const node of this._allNodes(root)) {
|
||||
if (node.tagName === tagName) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getElementsByTagName(tagName, root=null) {
|
||||
const results = [];
|
||||
for (const node of this._allNodes(root)) {
|
||||
if (node.tagName === tagName) {
|
||||
results.push(node);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
getElementsByClassName(className, root=null) {
|
||||
const results = [];
|
||||
const classNamePattern = new RegExp(`(^|\\s)${escapeRegExp(className)}(\\s|$)`);
|
||||
for (const node of this._allNodes(root)) {
|
||||
if (typeof node.tagName === 'string') {
|
||||
const nodeClassName = this.getAttribute(node, 'class');
|
||||
if (nodeClassName !== null && classNamePattern.test(nodeClassName)) {
|
||||
results.push(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
getAttribute(element, attribute) {
|
||||
for (const attr of element.attrs) {
|
||||
if (
|
||||
attr.name === attribute &&
|
||||
typeof attr.namespace === 'undefined'
|
||||
) {
|
||||
return attr.value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getTextContent(element) {
|
||||
let source = '';
|
||||
for (const node of this._allNodes(element)) {
|
||||
if (node.nodeName === '#text') {
|
||||
source += node.value;
|
||||
}
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
static isSupported() {
|
||||
return typeof parse5 !== 'undefined';
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
*_allNodes(root) {
|
||||
if (root === null) {
|
||||
root = this._document;
|
||||
}
|
||||
|
||||
// Depth-first pre-order traversal
|
||||
const nodeQueue = [root];
|
||||
while (nodeQueue.length > 0) {
|
||||
const node = nodeQueue.pop();
|
||||
|
||||
yield node;
|
||||
|
||||
const childNodes = node.childNodes;
|
||||
if (typeof childNodes !== 'undefined') {
|
||||
for (let i = childNodes.length - 1; i >= 0; --i) {
|
||||
nodeQueue.push(childNodes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -191,6 +191,33 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
</pre>
|
||||
</div></div></div></div></div>
|
||||
|
||||
<div data-show-for-manifest-version="3">
|
||||
<h2><a href="https://github.com/inikulin/parse5/blob/v6.0.1/LICENSE" target="_blank" rel="noopener noreferrer">parse5 v6.0.1</a></h2>
|
||||
<div class="settings-group"><div class="settings-item"><div class="settings-item-inner"><div class="settings-item-left"><div class="settings-item-label">
|
||||
<pre>
|
||||
Copyright (c) 2013-2019 Ivan Nikulin (ifaaan@gmail.com, https://github.com/inikulin)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
</pre>
|
||||
</div></div></div></div></div>
|
||||
</div>
|
||||
|
||||
<div class="footer-padding"></div>
|
||||
|
||||
</div>
|
||||
|
7985
ext/mixed/lib/parse5.js
Normal file
7985
ext/mixed/lib/parse5.js
Normal file
File diff suppressed because one or more lines are too long
@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
self.importScripts(
|
||||
'/mixed/lib/parse5.js',
|
||||
'/mixed/lib/wanakana.min.js',
|
||||
'/mixed/js/core.js',
|
||||
'/mixed/js/yomichan.js',
|
||||
@ -37,7 +38,7 @@ self.importScripts(
|
||||
'/bg/js/options.js',
|
||||
'/bg/js/profile-conditions.js',
|
||||
'/bg/js/request-builder.js',
|
||||
'/bg/js/native-simple-dom-parser.js',
|
||||
'/bg/js/simple-dom-parser.js',
|
||||
'/bg/js/text-source-map.js',
|
||||
'/bg/js/translator.js',
|
||||
'/bg/js/backend.js',
|
||||
|
@ -36,6 +36,13 @@ function getAllHtmlScriptPaths(fileName) {
|
||||
}
|
||||
}
|
||||
|
||||
function convertBackgroundScriptsToServiceWorkerScripts(scripts) {
|
||||
// Use parse5-based SimpleDOMParser
|
||||
scripts.splice(0, 0, '/mixed/lib/parse5.js');
|
||||
const index = scripts.indexOf('/bg/js/native-simple-dom-parser.js');
|
||||
assert.ok(index >= 0);
|
||||
scripts[index] = '/bg/js/simple-dom-parser.js';
|
||||
}
|
||||
|
||||
function main() {
|
||||
try {
|
||||
@ -45,6 +52,7 @@ function main() {
|
||||
const extDir = path.join(rootDir, extDirName);
|
||||
|
||||
const scripts = getAllHtmlScriptPaths(path.join(extDir, 'bg', 'background.html'));
|
||||
convertBackgroundScriptsToServiceWorkerScripts(scripts);
|
||||
const importedScripts = [];
|
||||
|
||||
const importScripts = (...scripts2) => {
|
||||
|
Loading…
Reference in New Issue
Block a user