PluginProbe
WProofreader spell & grammar check plugin for WordPress / trunk
WProofreader spell & grammar check plugin for WordPress vtrunk
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / assets / instance.js

instance.js in WProofreader spell & grammar check plugin for WordPress trunk, at assets/instance.js

110 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WProofreader Settings Page
3 * Initializes the spell checker instance and fetches languages.
4 */
5 jQuery(function ($) {
6 'use strict';
7
8 // Guards
9 if (typeof WEBSPELLCHECKER === 'undefined' || typeof ProofreaderInstance === 'undefined') return;
10
11 const serviceConfig = window.WSCServiceConfig || {};
12 const $root = $('#wsc_proofreader');
13 let $select = $root.find('select[name="wsc_proofreader[slang]"]');
14 if (!$select.length) return;
15
16 const enableGrammar = (ProofreaderInstance.enableGrammar === 'true');
17
18 // Init WebSpellChecker
19 const app = WEBSPELLCHECKER.initWebApi({
20 autoSearch: true,
21 serviceProtocol: serviceConfig.serviceProtocol || 'https',
22 serviceHost: serviceConfig.serviceHost || 'svc.webspellchecker.net',
23 servicePath: serviceConfig.servicePath || 'api',
24 servicePort: serviceConfig.servicePort || '443',
25 enableGrammar: enableGrammar,
26 serviceId: ProofreaderInstance.key_for_proofreader,
27 lang: ProofreaderInstance.slang,
28 appType: 'wp_plugin'
29 });
30 // Get info → send to WP → render languages
31 app.getInfo({
32 success(result) {
33 $.ajax({
34 type: 'POST',
35 url: window.ajaxurl,
36 data: {
37 action: 'get_proofreader_info_callback',
38 security: ProofreaderInstance.ajax_nonce,
39 getInfoResult: result
40 }
41 })
42 .done((res) => {
43 // Support both: raw HTML string OR {success:true, data:"..."} OR {success:true, data:{html:"..."}}
44 const html =
45 (typeof res === 'string') ? res :
46 (res && res.success && res.data && typeof res.data.html === 'string') ? res.data.html :
47 (res && res.success && typeof res.data === 'string') ? res.data : '';
48
49 if (!html) {
50 displayError('Failed to load language list. Invalid response format.');
51 return;
52 }
53
54 replaceLanguageSelect(html);
55 })
56 .fail((jqXHR, textStatus) => {
57 let msg = 'Failed to load language list.';
58 if (jqXHR && jqXHR.status === 403) {
59 msg = 'Your session has expired. Reload the page and try again.';
60 } else if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.data && jqXHR.responseJSON.data.message) {
61 msg = jqXHR.responseJSON.data.message;
62 } else if (textStatus) {
63 msg += ` (${textStatus})`;
64 }
65 displayError(msg);
66 });
67 },
68 error(err) {
69 displayError((err && err.message) || 'Unexpected initialization error.');
70 }
71 });
72
73 function replaceLanguageSelect(html) {
74 const nodes = $.parseHTML(html, document, false) || [];
75 const $newSelect = $(nodes).filter('select[name="wsc_proofreader[slang]"]').first();
76
77 if ($newSelect.length) {
78 $select.replaceWith($newSelect);
79 $select = $root.find('select[name="wsc_proofreader[slang]"]');
80 return;
81 }
82
83 const $options = $(nodes).filter('option');
84 if ($options.length) {
85 $select.empty().append($options);
86 return;
87 }
88
89 displayError('Failed to load language list. Invalid markup.');
90 }
91
92 function displayError(message) {
93 const html =
94 '<div class="error">' +
95 `<p><strong>WProofreader:</strong> ${escapeHtml(message)}</p>` +
96 '<p><a target="_blank" href="https://webspellchecker.com/contact-us/">Contact us</a> for technical assistance.</p>' +
97 '</div>';
98 $root.prepend(html);
99 }
100
101 function escapeHtml(str) {
102 return String(str)
103 .replace(/&/g, '&amp;')
104 .replace(/</g, '&lt;')
105 .replace(/>/g, '&gt;')
106 .replace(/"/g, '&quot;')
107 .replace(/'/g, '&#039;');
108 }
109 });
110