// ==UserScript==
// @name 💡WebPreview - 信息直达
// @namespace https://ez118.github.io/
// @version 0.9
// @description 支持国内主流搜索引擎的搜索结果快速预览(直达网页大纲)
// @author ZZY_WISU
// @match https://*.bing.com/*
// @match https://www.baidu.com/*
// @match https://www.so.com/*
// @match https://www.sogou.com/*
// @connect *
// @license GNU GPLv3
// @icon https://cn.bing.com/sa/simg/favicon-trans-bg-blue-mg.ico
// @run-at document-end
// @grant GM_xmlhttpRequest
// @grant GM_download
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js
// @downloadURL none
// ==/UserScript==
/* ============================================= */
/**
* 生成随机数
* @param len 长度
* @constructor
*/
function randomString(len) {
len = len || 32;
/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = $chars.length;
var pwd = '';
for (let i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); }
return pwd;
}
/**
* 标题元素
* @param tag 标签,h1 ==> 1 , h2 ==> 2
* @param title 标题
* @param level 层级
* @param id 随机生成的id
* @constructor
*/
function TitleElement(tag, title, level, id) {
this.tag = tag;
this.title = title;
this.level = level;
this.id = id;
}
/* 是否是标题元素 */
function isTitleTag(tag) {
if (tag.is("h1")) { return true }
if (tag.is("h2")) { return true }
if (tag.is("h3")) { return true }
if (tag.is("h4")) { return true }
if (tag.is("h5")) { return true }
if (tag.is("h6")) { return true }
if (tag.is("h7")) { return true }
return false
}
/**
* 生成大纲
* @param $articleContent 文章容器
* @constructor
*/
function getOutline($articleContent) {
/** 全部元素 */
var $eles = $articleContent.find("*");
/** 标题元素列表 */
var titleElementArr = new Array();
/** 上一个元素 */
var preTitleElement = null;
$.each($eles, function(index, item) {
if (isTitleTag($(item))) {
var id = randomString(20);
var level = 1;
var tag = parseInt($(item).get(0).tagName.replace('h', "").replace('H', ""));
var title = $(item).text();
if (null != preTitleElement) {
var tagPre = preTitleElement.tag;
var levelPre = preTitleElement.level;
if (tagPre > tag) { level = levelPre - 1; }
else if (tagPre < tag) { level = levelPre + 1; }
else { level = levelPre; }
}
if (title.trim().length > 0) {
$(item).attr("id", id);
var titleElement = new TitleElement(tag, title, level, id);
titleElementArr.push(titleElement);
preTitleElement = titleElement;
}
}
})
return titleElementArr;
}
/* ============================================= */
var ReaderFlag1 = false; /* 用于存储阅读器元素是否被创建 */
var ReaderFlag2 = false; /* 用于存储阅读器是否为开启状态 */
var VideoSupport = [
["https://v.youku.com/v_show/*.html", "https://player.youku.com/embed/*"],
["https://v.qq.com/x/page/*.html", "http://v.qq.com/txp/iframe/player.html?vid=*"],
["https://www.bilibili.com/video/BV*/", "https://www.bilibili.com/blackboard/html5mobileplayer.html?bvid=*"],
["https://www.bilibili.com/video/av*/", "https://www.bilibili.com/blackboard/html5mobileplayer.html?aid=*"]
]; /* 用于存储阅读器支持直接播放视频的网站及其嵌入播放器代码 */
function stringToHtml(str) {
var parser = new DOMParser();
var doc = parser.parseFromString(str, 'text/html');
return doc;
};
function runAsync(url,send_type,data_ry) {
var p = new Promise((resolve, reject)=> {
GM_xmlhttpRequest({
method: send_type, url: url, headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}, data: data_ry,
onload: function(response){resolve(response.responseText);}, onerror: function(response){reject("请求失败");}
});
});
return p;
}
function escapeHtml(str) {
return str.replace(/[&<>"']/g, function(match) {
switch (match) {
case '&':
//return '&';
return '&';
case '<':
return '<';
case '>':
return '>';
case '"':
return '"';
case '\'':
return ''';
default:
return '';
}
});
}
function JudgeVideoSupport(url) {
var previewFlag = 0;
/* 判断是否为支持预览视频的网站 */
for(let i = 0; i < VideoSupport.length; i ++){
if( url.includes( VideoSupport[i][0].split("*")[0] ) ){
return { "state":true, "data":i };
break;
}
}
return { "state":false, "data":-1 };
}
function getWebContents(txt) {
var links;
var images;
var content;
var outline;
/* 获取所有链接 */
/*links = txt.match(/href\=\"(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g);
for(let i = 0; i < links.length; i ++) {
links[i] = links[i].replace("href=\"", "");
}*/
links = [];
txt.replace(/]*href=['"]([^'"]+)[^>]*>/g,function(match, capture){
links.push(capture);
});
//console.log(links);
/* 获取所有图片 */
//images = txt.match(/]*src=[\"]([^\"]+)[\"][^<>]*>/im);
images = [];
txt.replace(/
]*src=['"]([^'"]+)[^>]*>/g,function(match, capture){
images.push(capture);
});
//console.log(images);
/* 获取文本 */
content = txt.replace(/<\/div>/g, "\n").replace(/<\/table>/g, "\n")
content = content.replace(/<\/h3>/g, "\n").replace(/<\/p>/g, "