-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgrammarly.el
362 lines (309 loc) · 12.2 KB
/
grammarly.el
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;;; grammarly.el --- Grammarly API interface -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2025 Shen, Jen-Chieh
;; Created date 2019-11-06 20:41:48
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-grammarly/grammarly
;; Version: 0.3.0
;; Package-Requires: ((emacs "26.1") (s "1.12.0") (request "0.3.0") (websocket "1.6"))
;; Keywords: convenience grammar api interface english
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Grammarly API interface.
;;
;;; Code:
(require 'cl-lib)
(require 's)
(require 'subr-x)
(require 'json)
(require 'request)
(require 'websocket)
(defgroup grammarly nil
"Grammarly API interface."
:prefix "grammarly-"
:group 'text
:link '(url-link :tag "Github" "https://github.com/emacs-grammarly/grammarly"))
(defcustom grammarly-username ""
"Grammarly login username."
:type 'string
:group 'grammarly)
(defcustom grammarly-password ""
"Grammarly login password."
:type 'string
:group 'grammarly)
(defconst grammarly--user-agent
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0"
"User agent.")
(defconst grammarly--browser-headers
`(("User-Agent" . ())
("Accept" . "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
("Accept-Language" . "en-GB,en-US;q=0.9,en;q=0.8")
("Cache-Control" . "no-cache")
("Pragma" . "no-cache"))
"Header for simulate using a browser.")
(defconst grammarly--authorize-msg
`(("origin" . "chrome-extension://kbfnbcaeplbcioakkpcpgfkobkghlhen")
("Cookie" . "$COOKIES$")
("User-Agent" . ,grammarly--user-agent))
"Authorize message for Grammarly API.")
(defconst grammarly--init-msg
'(("type" . "initial")
("token" . ())
("docid" . "dfad0927-7b35-e155-6de9-4a107053da35-43543554345")
("client" . "extension_chrome")
("protocolVersion" . "1.0")
("clientSupports" . ("free_clarity_alerts"
"readability_check"
"filler_words_check"
"sentence_variety_check"
"free_occasional_premium_alerts"))
("dialect" . "american")
("clientVersion" . "14.924.2437")
("extDomain" . "editpad.org")
("action" . "start")
("id" . 0))
"Grammarly initialize message for verify use.")
(defconst grammarly--request-check
'(("ch" . ("+0:0:$STR$:0"))
("rev" . 0)
("action" . "submit_ot")
("id" . 0))
"Grammarly request package definition.")
(defvar grammarly-on-message-function-list nil
"List of callback function when execute on message.")
(defvar grammarly-on-open-function-list nil
"List of callback function when execute on open.")
(defvar grammarly-on-close-function-list nil
"List of callback function when execute on close.")
(defvar grammarly--text ""
"Current text that are going to check for.")
(defvar grammarly--client nil
"Websocket for this client.")
(defvar grammarly--update-time 0.1
"Run every this seconds until we received API request.")
(defvar grammarly--cookies ""
"Record the cookie down.")
(defvar grammarly--timer nil
"Universal timer for each await use.")
(defvar grammarly--start-checking-p nil
"Flag to after we are done preparing; basically after authentication process.")
(defvar grammarly--show-debug-message nil
"Flag to see if we show debug messages.")
;;
;; (@* "Util" )
;;
(defun grammarly--debug-message (fmt &rest args)
"Debug message like function `message' with same argument FMT and ARGS."
(when grammarly--show-debug-message
(apply 'message fmt args)))
(defun grammarly--kill-websocket ()
"Kill the websocket."
(when grammarly--client
(websocket-close grammarly--client)
(setq grammarly--client nil)))
(defun grammarly--kill-timer ()
"Kill the timer."
(when (timerp grammarly--timer)
(cancel-timer grammarly--timer)
(setq grammarly--timer nil)))
(defun grammarly--execute-function-list (lst &rest args)
"Execute all function LST with ARGS."
(cond
((functionp lst) (apply lst args))
((listp lst) (dolist (fnc lst) (apply fnc args)))
(t (user-error "[ERROR] Function does not exists: %s" lst))))
(defun grammarly-load-from-authinfo (&optional username)
"Load Grammarly authentication info from auth-source.
Optionally pass the USERNAME, otherwise, it will be searched in the authinfo
file. You will need to add a line in your authinfo file \"machine grammarly.com
login <YOUR-EMAIL> pass <YOUR-PASSWORD>\"."
(require 'auth-source)
(when-let* ((user-info (car (auth-source-search :host "grammarly.com" :user username)))
(user (or username (plist-get user-info :user)))
(secret (plist-get user-info :secret)))
(setq grammarly-username user
grammarly-password (funcall secret))
t))
;;
;; (@* "Cookie" )
;;
(defvar grammarly--auth-cookie nil
"Authorization cookie container.")
(defun grammarly--last-cookie (cookie cookies)
"Check if current COOKIE the last cookie from COOKIES."
(equal (nth (1- (length cookies)) cookies) cookie))
(defun grammarly--get-cookie-by-name (name)
"Return cookie value by cookie NAME."
(let ((len (length grammarly--auth-cookie)) (index 0) break cookie-val)
(while (and (not break) (< index len))
(let* ((cookie (nth index grammarly--auth-cookie))
(cookie-name (car cookie)))
(when (string= cookie-name name)
(setq cookie-val (cdr cookie)
break t)))
(setq index (1+ index)))
cookie-val))
(defun grammarly--form-cookie ()
"Form all cookies into one string."
(setq grammarly--auth-cookie nil)
(let ((sec-cookies (request-cookie-alist ".grammarly.com" "/" t))
(cookie-str "") cookie-name cookie-val)
(dolist (cookie sec-cookies)
(setq cookie-name (car cookie) cookie-val (cdr cookie)
cookie-str
(format "%s %s=%s%s" cookie-str cookie-name cookie-val
(if (grammarly--last-cookie cookie sec-cookies) "" ";")))
(push (cons cookie-name cookie-val) grammarly--auth-cookie))
(setq grammarly--auth-cookie (reverse grammarly--auth-cookie))
(string-trim cookie-str)))
(defun grammarly--update-cookie ()
"Refresh the cookie once."
(setq grammarly--cookies (grammarly--form-cookie)))
(defun grammarly--get-cookie ()
"Get cookie."
(setq grammarly--start-checking-p nil
grammarly--cookies "") ; Reset to clean string.
(request
"https://grammarly.com/signin"
:type "GET"
:headers
(append grammarly--browser-headers
'(("Sec-Fetch-Mode" . "navigate")
("Sec-Fetch-Sit" . "same-origin")
("Sec-Fetch-User" . "?1")
("Upgrade-Insecure-Requests" . "1")
("Referer" . "https://www.grammarly.com/")))
:success
(cl-function
(lambda (&key _response &allow-other-keys)
(grammarly--update-cookie)
(if (grammarly-premium-p) ; Try login to use paid version.
(grammarly--authenticate)
(setq grammarly--start-checking-p t))))
:error
;; NOTE: Accept, error.
(cl-function
(lambda (&rest args &key _error-thrown &allow-other-keys)
(grammarly--debug-message "[ERROR] Error while getting cookie: %s" args)))))
;;
;; (@* "Login" )
;;
(defun grammarly-premium-p ()
"Return non-nil means we are using premium version."
(and (not (string-empty-p grammarly-username))
(not (string-empty-p grammarly-password))))
(defun grammarly--authenticate ()
"Login to Grammarly for premium version."
(message "connecting as %s" grammarly-username)
(request
"https://auth.grammarly.com/v3/api/login"
:type "POST"
:headers
`(("accept" . "application/json")
("accept-language" . "en-GB,en-US;q=0.9,en;q=0.8")
("content-type" . "application/json")
("user-agent" . ,grammarly--user-agent)
("x-client-type" . "funnel")
("x-client-version" . "1.2.2026")
("x-container-id" . ,(grammarly--get-cookie-by-name "gnar_containerId"))
("x-csrf-token" . ,(grammarly--get-cookie-by-name "csrf-token"))
("sec-fetch-site" . "same-site")
("sec-fetch-mode" . "cors")
("cookie" . ,(format "gnar_containrId=%s; grauth=%s; csrf-token=%s"
(grammarly--get-cookie-by-name "gnar_containerId")
(grammarly--get-cookie-by-name "grauth")
(grammarly--get-cookie-by-name "csrf-token"))))
:data
(json-encode
`(("email_login" . (("email" . ,grammarly-username)
("password" . ,grammarly-password)
("secureLogin" . "false")))))
:success
(cl-function
(lambda (&key _response &allow-other-keys)
(setq grammarly--start-checking-p t)))
:error
;; NOTE: Accept, error.
(cl-function
(lambda (&rest args &key _error-thrown &allow-other-keys)
(setq grammarly--start-checking-p t) ; Go back and use anonymous version
(grammarly--debug-message
"[ERROR] Error while authenticating login: %s" args)))))
;;
;; (@* "WebSocket" )
;;
(defun grammarly--form-authorize-list ()
"Form the authorize list."
(let ((auth (copy-sequence grammarly--authorize-msg)))
;; NOTE: Here we directly point it to the `$COOKIES$' keyword.
(setcdr (nth 1 auth) grammarly--cookies)
auth))
(defun grammarly--form-check-request (text)
"Form a check request by TEXT."
(let ((req (copy-sequence grammarly--request-check)))
;; NOTE: Here we directly point it to the `$STR$' keyword.
(setf (nth 0 (cdr (nth 0 req))) (s-replace "$STR$" text "+0:0:$STR$:0"))
req))
(defun grammarly--after-got-cookie ()
"Execution after received all needed cookies."
(grammarly--kill-websocket)
(setq
grammarly--client
(websocket-open
"wss://capi.grammarly.com/freews"
:custom-header-alist (grammarly--form-authorize-list)
:on-open
(lambda (_ws)
(grammarly--execute-function-list grammarly-on-open-function-list)
;; Verify valid client connection.
(websocket-send-text grammarly--client (json-encode grammarly--init-msg))
(websocket-send-text grammarly--client (json-encode (grammarly--form-check-request grammarly--text))))
:on-message
(lambda (_ws frame)
(grammarly--execute-function-list grammarly-on-message-function-list (websocket-frame-payload frame))
(grammarly--default-callback (websocket-frame-payload frame)))
:on-error
(lambda (_ws _type err)
(grammarly--debug-message
"[ERROR] Connection error while opening websocket: %s" err))
:on-close
(lambda (_ws)
(grammarly--execute-function-list grammarly-on-close-function-list)))))
;;
;; (@* "Core" )
;;
(defun grammarly--reset-timer (fnc pred)
"Reset the timer for the next run with FNC and PRED."
(grammarly--kill-timer)
(if (funcall pred)
(setq grammarly--timer
(run-with-timer grammarly--update-time nil
'grammarly--reset-timer fnc pred))
(funcall fnc)))
(defun grammarly--default-callback (data)
"Default callback, print out DATA."
(when (string-match-p "\"action\":\"finished\"" data)
;; Clean up after last response action received.
(grammarly--kill-websocket)))
;;;###autoload
(defun grammarly-check-text (text)
"Send the TEXT to check."
(if (or (not (stringp text)) (string-empty-p text))
(user-error "[ERROR] Text can't be 'nil' or 'empty'")
(setq grammarly--text text)
(grammarly--get-cookie)
;; Delay, until we get the initial cookie.
(grammarly--reset-timer #'grammarly--after-got-cookie
'(lambda () (null grammarly--start-checking-p)))))
(provide 'grammarly)
;;; grammarly.el ends here