-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.html
executable file
·116 lines (100 loc) · 4.78 KB
/
index.html
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="mastodon.js"></script>
<script>
// initialize library
// api_user_token has not to be set at this point if you don't have one yet
// NOTE: It needs jQuery!
var api = new MastodonAPI({
instance: "https://pleasehug.me",
api_user_token: ""
});
//
// BASIC REST FUNCTIONS
// you can use every api endpoint from https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md
//
api.post("statuses", {status:"i like pineapple on pizza"}, function (data) {
// will be called if the toot was successful
// data is the http response object
//sidenote: please do not actually execute this request, you could be bullied by your friends
});
api.get("accounts/1/following", {
since_id: 420,
max_id: 1337
}, function(data) {
// returns all users account id 1 is following in the id range from 420 to 1337
// you don't have to supply the parameters, you can also just go with .get(endpoint, callback)
});
api.delete("statuses/1", function (data) {
//deletes toot with id 1
});
//
// STREAMING
//
//lets initialize a stream! stream types are
// user for your local home TL and notifications
// public for your federated TL
// public:local for your home TL
// hashtag&tag=fuckdonaldtrump for the stream of #fuckdonaldtrump
api.stream("user", function(data) {
// data is an object containing two entries
// event determines which type of data you got
// payload is the actual data
// event can be notification or update
if (data.event === "notification") {
// data.payload is a notification
} else if (data.event === "update") {
// status update for one of your timelines
} else {
// probably an error
}
})
// AUTH
// first, we register our application
// for this we need no key since we don't have one yet
api.registerApplication("my cool application name",
window.location.href, // redirect uri, we will need this later on
["read", "write", "follow"], //scopes
"http://mycoolwebsite.com", //website on the login screen
function(data) {
// we got our application
// lets save it to our browser storage
localStorage.setItem("mastodon_client_id", data["client_id"]);
localStorage.setItem("mastodon_client_secret", data["client_secret"]);
localStorage.setItem("mastodon_client_redirect_uri", data["redirect_uri"]);
// now, that we have saved our application data, generate an oauth url and send
// our user to it!
window.location.href = api.generateAuthLink(data["client_id"],
data["redirect_uri"],
"code", // oauth method
["read", "write", "follow"] //scopes
);
}
);
// now we get to the part why the redirect uri was set to our current location
// we can just access to code from the url hash
if (window.location.href.indexOf("?code=") !== -1) {
// nice, we got our auth code!
// lets put it into a variable
var authCode = window.location.href.replace(window.location.origin + window.location.pathname + "?code=", "");
// nice variable clusterfuck, eh?
// we have everything needed to access our oauth token
api.getAccessTokenFromAuthCode(
localStorage.getItem("mastodon_client_id"),
localStorage.getItem("mastodon_client_secret"),
localStorage.getItem("mastodon_client_redirect_uri"),
authCode,
function(data) {
// AAAND DATA CONTAINS OUR TOKEN!
// use api.setConfig("api_user_token", tokenvar) to set it without having to reinit the entire
// library.
}
)
}
</script>
</head>
<body>
</body>
</html>