Coder Social home page Coder Social logo

codecopy / tampermonkey- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ahaochan/tampermonkey

0.0 1.0 0.0 186 KB

油猴脚本集合

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

License: GNU General Public License v3.0

JavaScript 100.00%

tampermonkey-'s Introduction

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");
    });
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.