tampermonkey脚本示例包括加载jquery


最近因为使用firefox-esr导致油猴子无法使用了。后来发现tampermonkey就试用一下。发现还真不错,最主要的是引入外部js(如jquery)文件不用想greasemonkey那么麻烦了。就跟正常项目中编写js脚本一样,非常方便。下面是测试代码。

// ==UserScript==
// @name         Tampermonkey测试脚本
// @namespace    http:///
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http:///[target_uri]/*
// @grant        none
// @require      https://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==
console.log('start');
$(document).ready(function(){
    console.log('ready');
    $.fn.floatAutoScroll = function () {
            this.each(function () {
                var obj = $(this);
                var top = obj.css('top').replace('px', '');
                $(window).scroll(function () {
                    var scrollTop = $(window).scrollTop();
                    obj.stop().animate({top: (top * 1 + scrollTop * 1)}, 'normal');
                });
            });
        }
    function run(){
        alert('hello');
    }
    console.log('add button');
    var btnSpider = document.createElement('div');
        btnSpider.id = 'wk_btn';
        btnSpider.style='display:block;z-index:999;position:absolute; right:10px;top:45%; width:60px; height:60px;line-height:30px; background-color:#f50;color:#fff;text-align:center;font-size:16px;font-family:"Microsoft YaHei","微软雅黑",STXihei,"华文细黑",Georgia,"Times New Roman",Arial,sans-serif;font-weight:bold;cursor:pointer';
        btnSpider.innerHTML='点&nbsp;&nbsp;我<br>开&nbsp;&nbsp;始';
        $('body').append(btnSpider);

        $('#wk_btn').floatAutoScroll();
        $('#wk_btn').on('click',function(){
            $('#wk_next').hide();
            run();
        });
    console.log('end');
});
console.log('complete');
普人特福的博客cnzz&51la for wordpress,cnzz for wordpress,51la for wordpress