vivicai / Tampermonkey

油猴脚本集合

Home Page:https://greasyfork.org/zh-CN/users/30831

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Greasy Fork

Greasy Fork脚本集合

1、函数在页面中点击执行

在脚本中定义函数function abc(){ alert("helloWorld"); },注入onclick事件<a id="a" onclick="abc();">HelloWorld</a>。 爆出函数未定义的错误Function is not defined。 在mozillazine了解到Tampermonkey的js脚本是在sandbox中的,在html中访问不到。 使用下面的例子可以完成这个功能

unsafeWindow.abc = function(msg) {
  alert(msg);
};
document.getElementById("a").onclick = "window.abc('helloWorld')";

2、跨域访问

http://m.imooc.com/video/14388中底部写死了mp4url,判断依据是禁用js仍然在html源代码中发现了这个url。 课程所在位置是http://www.imooc.com/learn/814。属于跨域访问的问题,使用GM_xmlhttpRequest可以解决。

3、模拟事件

来自stackoverflow,原生js实现的模拟点击事件. trigger对非JQuery绑定的事件无效。

$('#downTip').click(function(){
	//$('#js-signin-btn').trigger('click');
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    document.getElementById('js-signin-btn').dispatchEvent (clickEvent);
});

4、拦截Ajax请求的url路径

(function (open) {
    XMLHttpRequest.prototype.open = function () {
        this.addEventListener("readystatechange", function () {
            if (this.responseURL.indexOf('.hxk') >= 0) {
                console.log(this.responseURL);
            }

        }, false);
        open.apply(this, arguments);
    };
})(XMLHttpRequest.prototype.open);

5、Ajax跨域请求多图并压缩为Zip

依赖JSZipFileSaver, 具体实现参考Pixiv-增强

var zip = new JSZip();
for (var i = 0; i < 16; i++) {
    (function (index) {
        var url = 'https://i.pximg.net/img-master/img' + param + '_p' + index + '_master1200.jpg';
        GM_xmlhttpRequest({
            method: 'GET',
            headers: {referer: 'https://www.pixiv.net/'}, // pixiv加了防盗链referer
            overrideMimeType: 'text/plain; charset=x-user-defined',
            url: url,
            onload: function (xhr) {
                var r = xhr.responseText, data = new Uint8Array(r.length), i = 0;
                while (i < r.length) {
                    data[i] = r.charCodeAt(i);
                    i++;
                }
                var blob = new Blob([data], {type: 'image/jpeg'}); // 转为Blob类型

                zip.file('pic_' + index + '.jpg', blob, {binary: true}); // 压入zip中
            }
        });
    })(i);
}

// 注意GM_xmlhttpRequest的ajax请求不是同步的
$('按钮').click(function(){
    zip.generateAsync({type: "blob", base64: true}).then(function (content) {
        // see FileSaver.js'
        saveAs(content, "pic.zip");
    });
});

About

油猴脚本集合

https://greasyfork.org/zh-CN/users/30831

License:GNU General Public License v3.0


Languages

Language:JavaScript 100.0%