mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
o
This commit is contained in:
parent
30c56a55a1
commit
9e24c64e1b
3 changed files with 122 additions and 4 deletions
108
searx/static/themes/magi/Readability-readerable.js
Normal file
108
searx/static/themes/magi/Readability-readerable.js
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
/* eslint-env es6:false */
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010 Arc90 Inc
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This code is heavily based on Arc90's readability.js (1.7.1) script
|
||||||
|
* available at: http://code.google.com/p/arc90labs-readability
|
||||||
|
*/
|
||||||
|
|
||||||
|
var REGEXPS = {
|
||||||
|
// NOTE: These two regular expressions are duplicated in
|
||||||
|
// Readability.js. Please keep both copies in sync.
|
||||||
|
unlikelyCandidates: /-ad-|ai2html|banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|footer|gdpr|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i,
|
||||||
|
okMaybeItsACandidate: /and|article|body|column|content|main|shadow/i,
|
||||||
|
};
|
||||||
|
|
||||||
|
function isNodeVisible(node) {
|
||||||
|
// Have to null-check node.style and node.className.indexOf to deal with SVG and MathML nodes.
|
||||||
|
return (!node.style || node.style.display != "none")
|
||||||
|
&& !node.hasAttribute("hidden")
|
||||||
|
//check for "fallback-image" so that wikimedia math images are displayed
|
||||||
|
&& (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true" || (node.className && node.className.indexOf && node.className.indexOf("fallback-image") !== -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decides whether or not the document is reader-able without parsing the whole thing.
|
||||||
|
* @param {Object} options Configuration object.
|
||||||
|
* @param {number} [options.minContentLength=140] The minimum node content length used to decide if the document is readerable.
|
||||||
|
* @param {number} [options.minScore=20] The minumum cumulated 'score' used to determine if the document is readerable.
|
||||||
|
* @param {Function} [options.visibilityChecker=isNodeVisible] The function used to determine if a node is visible.
|
||||||
|
* @return {boolean} Whether or not we suspect Readability.parse() will suceeed at returning an article object.
|
||||||
|
*/
|
||||||
|
function isProbablyReaderable(doc, options = {}) {
|
||||||
|
// For backward compatibility reasons 'options' can either be a configuration object or the function used
|
||||||
|
// to determine if a node is visible.
|
||||||
|
if (typeof options == "function") {
|
||||||
|
options = { visibilityChecker: options };
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultOptions = { minScore: 20, minContentLength: 140, visibilityChecker: isNodeVisible };
|
||||||
|
options = Object.assign(defaultOptions, options);
|
||||||
|
|
||||||
|
var nodes = doc.querySelectorAll("p, pre, article");
|
||||||
|
|
||||||
|
// Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
|
||||||
|
// Some articles' DOM structures might look like
|
||||||
|
// <div>
|
||||||
|
// Sentences<br>
|
||||||
|
// <br>
|
||||||
|
// Sentences<br>
|
||||||
|
// </div>
|
||||||
|
var brNodes = doc.querySelectorAll("div > br");
|
||||||
|
if (brNodes.length) {
|
||||||
|
var set = new Set(nodes);
|
||||||
|
[].forEach.call(brNodes, function (node) {
|
||||||
|
set.add(node.parentNode);
|
||||||
|
});
|
||||||
|
nodes = Array.from(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
var score = 0;
|
||||||
|
// This is a little cheeky, we use the accumulator 'score' to decide what to return from
|
||||||
|
// this callback:
|
||||||
|
return [].some.call(nodes, function (node) {
|
||||||
|
if (!options.visibilityChecker(node)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var matchString = node.className + " " + node.id;
|
||||||
|
if (REGEXPS.unlikelyCandidates.test(matchString) &&
|
||||||
|
!REGEXPS.okMaybeItsACandidate.test(matchString)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.matches("li p")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var textContentLength = node.textContent.trim().length;
|
||||||
|
if (textContentLength < options.minContentLength) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
score += Math.sqrt(textContentLength - options.minContentLength);
|
||||||
|
|
||||||
|
if (score > options.minScore) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof module === "object") {
|
||||||
|
module.exports = isProbablyReaderable;
|
||||||
|
}
|
|
@ -854,6 +854,7 @@ def search():
|
||||||
|
|
||||||
<div id="iframe-wrapper">
|
<div id="iframe-wrapper">
|
||||||
<iframe ></iframe>
|
<iframe ></iframe>
|
||||||
|
<div id='readability-reader'></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -866,6 +867,9 @@ def search():
|
||||||
// 3. 点击 closeBtn 就隐藏 mask 和 modal
|
// 3. 点击 closeBtn 就隐藏 mask 和 modal
|
||||||
closeBtn.addEventListener('click', function () {
|
closeBtn.addEventListener('click', function () {
|
||||||
modal.style.display = 'none';
|
modal.style.display = 'none';
|
||||||
|
document.querySelector("#readability-reader").innerHTML = '';
|
||||||
|
try{iframe.removeAttribute('src');}catch(e){}
|
||||||
|
|
||||||
})
|
})
|
||||||
// 4. 开始拖拽
|
// 4. 开始拖拽
|
||||||
// (1) 当我们鼠标按下, 就获得鼠标在盒子内的坐标
|
// (1) 当我们鼠标按下, 就获得鼠标在盒子内的坐标
|
||||||
|
@ -1144,7 +1148,7 @@ button.btn_more {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script src="/static/themes/magi/Readability-readerable.js"></script>
|
||||||
<script src="/static/themes/magi/Readability.js"></script>
|
<script src="/static/themes/magi/Readability.js"></script>
|
||||||
<script src="/static/themes/magi/markdown.js"></script>
|
<script src="/static/themes/magi/markdown.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
@ -1192,7 +1196,8 @@ function modal_open(url)
|
||||||
() => {
|
() => {
|
||||||
if (isProbablyReaderable(iframe.contentDocument)) {
|
if (isProbablyReaderable(iframe.contentDocument)) {
|
||||||
let article = new Readability(iframe.contentDocument.cloneNode(true)).parse();
|
let article = new Readability(iframe.contentDocument.cloneNode(true)).parse();
|
||||||
iframe.contentDocument.documentElement.innerHTML = article.content
|
iframe.removeAttribute('src')
|
||||||
|
document.querySelector("#readability-reader").innerHTML = article.content
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
|
|
|
@ -854,6 +854,7 @@ def search():
|
||||||
|
|
||||||
<div id="iframe-wrapper">
|
<div id="iframe-wrapper">
|
||||||
<iframe ></iframe>
|
<iframe ></iframe>
|
||||||
|
<div id='readability-reader'></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -866,6 +867,9 @@ def search():
|
||||||
// 3. 点击 closeBtn 就隐藏 mask 和 modal
|
// 3. 点击 closeBtn 就隐藏 mask 和 modal
|
||||||
closeBtn.addEventListener('click', function () {
|
closeBtn.addEventListener('click', function () {
|
||||||
modal.style.display = 'none';
|
modal.style.display = 'none';
|
||||||
|
document.querySelector("#readability-reader").innerHTML = '';
|
||||||
|
try{iframe.removeAttribute('src');}catch(e){}
|
||||||
|
|
||||||
})
|
})
|
||||||
// 4. 开始拖拽
|
// 4. 开始拖拽
|
||||||
// (1) 当我们鼠标按下, 就获得鼠标在盒子内的坐标
|
// (1) 当我们鼠标按下, 就获得鼠标在盒子内的坐标
|
||||||
|
@ -1144,7 +1148,7 @@ button.btn_more {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script src="/static/themes/magi/Readability-readerable.js"></script>
|
||||||
<script src="/static/themes/magi/Readability.js"></script>
|
<script src="/static/themes/magi/Readability.js"></script>
|
||||||
<script src="/static/themes/magi/markdown.js"></script>
|
<script src="/static/themes/magi/markdown.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
@ -1192,7 +1196,8 @@ function modal_open(url)
|
||||||
() => {
|
() => {
|
||||||
if (isProbablyReaderable(iframe.contentDocument)) {
|
if (isProbablyReaderable(iframe.contentDocument)) {
|
||||||
let article = new Readability(iframe.contentDocument.cloneNode(true)).parse();
|
let article = new Readability(iframe.contentDocument.cloneNode(true)).parse();
|
||||||
iframe.contentDocument.documentElement.innerHTML = article.content
|
iframe.removeAttribute('src')
|
||||||
|
document.querySelector("#readability-reader").innerHTML = article.content
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue