-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext.js
105 lines (96 loc) · 3.22 KB
/
context.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
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
function publish(content, font) {
if (content.trim() == "") {
return;
}
var post = H.getTitleStrict(content);
var http = new XMLHttpRequest();
var url = "https://write.as/api/posts";
var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
lang = lang.substring(0, 2);
var params = "body=" + encodeURIComponent(post.content) + "&title=" + encodeURIComponent(post.title) + "&font=" + font + "&lang=" + lang + "&rtl=auto";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if (http.readyState == 4) {
if (http.status == 201) {
data = JSON.parse(http.responseText);
// Pull out data parts
id = data.data.id;
if (font == 'code' || font === 'mono') {
url = "https://paste.as/"+id;
} else {
url = "https://write.as/"+id;
}
editToken = data.data.token;
// Save the data if user wasn't logged in
if (typeof data.data.owner === 'undefined' || data.data.owner == "") {
posts = JSON.parse(H.get('posts', '[]'));
posts.push(H.createPost(id, editToken, post.content));
H.set('posts', JSON.stringify(posts));
}
// Launch post
chrome.tabs.create({ url: url });
} else {
alert("Failed to post. Please try again.");
}
}
}
http.send(params);
}
function getSelectedText(callback) {
// Workaround since info.selectionText in context menu click handler doesn't
// preserve newlines.
// Source: https://code.google.com/p/chromium/issues/detail?id=116429#c11
chrome.tabs.executeScript({
code: "window.getSelection().toString();"
}, function(selection) {
callback(selection[0]);
});
}
chrome.contextMenus.create({"title": "Publish text (sans)", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
getSelectedText(function(sel) {
publish(sel, "sans");
});
}
});
chrome.contextMenus.create({"title": "Publish text (serif)", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
getSelectedText(function(sel) {
publish(sel, "norm");
});
}
});
chrome.contextMenus.create({"title": "Publish code", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
getSelectedText(function(sel) {
publish(sel, "code");
});
}
});
chrome.runtime.onMessageExternal.addListener(function(req, sender, callback) {
if (req) {
if (req.msg) {
if (req.msg == "ping") {
callback("pong");
} else if (req.msg == "posts") {
callback(JSON.parse(H.get('posts', '[]')));
} else if (req.msg == "deletePosts" && req.data && req.data.length > 0) {
// Delete all posts listed in req.data, an array of post IDs.
var posts = JSON.parse(H.get('posts', '[]'));
var exportedPosts = [];
for (var i=0; i<req.data.length; i++) {
for (var j=0; j<posts.length; j++) {
if (posts[j].id === req.data[i]) {
console.log("Removing post " + req.data[i]);
exportedPosts.push(posts.splice(j, 1));
}
}
}
if (exportedPosts.length > 0) {
H.set('posts', JSON.stringify(posts));
H.set('exportedPosts', JSON.stringify(exportedPosts));
}
}
}
}
return true;
});