Skip to content

Instantly share code, notes, and snippets.

@wayou
Last active January 13, 2019 08:28
Show Gist options
  • Save wayou/0a230871b830bd02f1e509346b182aed to your computer and use it in GitHub Desktop.
Save wayou/0a230871b830bd02f1e509346b182aed to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name baidu yun 资源链接
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 自动将google搜索结果页面中百度云盘地址变成可点击的链接,如果链接附近有提取码,在点击时自动复制
// @author You
// @match https://www.google.com/search*
// @grant none
// ==/UserScript==
(function() {
"use strict";
window.copyToClp = function(txt) {
txt = document.createTextNode(txt);
var m = document;
var w = window;
var b = m.body;
b.appendChild(txt);
if (b.createTextRange) {
var d = b.createTextRange();
d.moveToElementText(txt);
d.select();
m.execCommand("copy");
} else {
var d = m.createRange();
var g = w.getSelection;
d.selectNodeContents(txt);
g().removeAllRanges();
g().addRange(d);
m.execCommand("copy");
g().removeAllRanges();
}
txt.remove();
};
const pattern = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,})(\s*)((密码|提取码)[\:|:]?(\s*)([a-zA-Z0-9]{4}))?/g;
document.querySelectorAll("#search span").forEach(ele => {
const text = ele.textContent;
const html = text.replace(pattern, (m, g1, g2, g3, g4, g5, g6) => {
let pswd = g6 || "";
let link = g1;
return `<a href="${link}" target="_blank" onclick="copyToClp('${pswd}')">${link}</a>${g2 ||
""}${g3 || ""}`;
});
ele.innerHTML = html;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment