油猴脚本集合
在脚本中定义函数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
中底部写死了mp4
的url
,判断依据是禁用js
仍然在html
源代码中发现了这个url
。
课程所在位置是http://www.imooc.com/learn/814
。属于跨域访问的问题,使用GM_xmlhttpRequest可以解决。
来自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
依赖JSZip、FileSaver, 具体实现参考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"); }); });
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。