最近看了点js,然后发现feedly分享功能要收费,记得Tampermonkey可以在页面外自己定义js,所以用js自己做了一个,还是有点意思的,下面是对步骤的一些记录。
首先必须要在chrome安装Tampermonkey,略。
然后就是Tampermonkey的使用了。
Tampermonkey主要分为了两个部分:注释段和代码段。
它的注释段也是有用的,提供了一些脚本执行的参数,所以需要按照一定的格式书写,注释段比较常用的几个参数是:

  • @name The name of the script. 脚本名字,保存的时候就不再需要另命名了。
  • @namespace The namespace of the script.避免和别的js中的名称冲突,如果以http://开头同时会被当作作者的主页。
  • @match http://* More or less equal to the @include tag.
  • @include The pages on that a script should run.
  • @require Points to a JavaScript file that is loaded and executed before the script itself starts running.

代码段就是正常的js代码了,可以当作是在原始页面加载完了后,就开始加载这段代码段。
下面是完整的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ==UserScript==
// @name CustomShare
// @namespace http://liu-uil.com/
// @version 0.1
// @description try to take over the world!
// @author yangliu
// @match http://*
// @match https://*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// @include http:/*
// @include https://*
// ==/UserScript==
// Your code here...
ImportCss();
Share();
function ImportCss() {
var jqueryScriptBlock = document.createElement('style');
jqueryScriptBlock.type = 'text/css';
jqueryScriptBlock.innerHTML = "#share {position:fixed;bottom:30%;right:1px;border:1px solid gray;padding:3px;width:12px;font-size:12px;cursor:pointer;border-radius: 3px;text-shadow: 1px 1px 3px #676767;}";
document.getElementsByTagName('head')[0].appendChild(jqueryScriptBlock);
}
function Share() {
$(document.body).append("<div id='share' >分享</div>");
$('#share').click(function () {
var param = {
url: window.location.href,
title: $("title").text()+'\n\n——转发自自定义分享按钮',
};
window.open(getLink("http://service.weibo.com/share/share.php?", param));
});
}
function getLink(link, param) {
return link + addParam(param).join('&');
}
function addParam(param) {
var temp = [];
for (var p in param) {
temp.push(p + '=' + encodeURIComponent(param[p] || ''))
}
return temp;
}