From 4fe06d5c600fc3e3dc1a4173ae404aa6b7fc1da5 Mon Sep 17 00:00:00 2001 From: max104t Date: Fri, 16 Aug 2019 12:57:16 -0700 Subject: [PATCH] Fix Apitest for userscript --- ApiTest.html | 29 ------------------ ApiTest.js | 86 ++++++++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 72 deletions(-) delete mode 100644 ApiTest.html diff --git a/ApiTest.html b/ApiTest.html deleted file mode 100644 index 6689985..0000000 --- a/ApiTest.html +++ /dev/null @@ -1,29 +0,0 @@ -
Fork me on GitHubSome documentation can be found in docs by @cjhbtn
-
Url
-

-
Http


-
Method
-

-
Header

-
Parameters


- Submit

-
Response is also displayed in the browser console, if [Debug mode] is checked
-    
diff --git a/ApiTest.js b/ApiTest.js index 74b550b..e09e2f2 100644 --- a/ApiTest.js +++ b/ApiTest.js @@ -1,48 +1,48 @@ var ApiTestController = { init: function(parent) { - NODEJS = typeof require === 'function'; - if (NODEJS) { - ApiTestController.init_nodejs(parent); - } else { - ApiTestController.init_one_script(parent); - } - }, - init_nodejs: function(parent) { - const fs = require('fs'); - fs.readFile('ApiTest.html', 'utf8', function (err, data) { - parent.append(data); - }); - }, - init_one_script: function(parent) { - parent.append('
Some documentation can be found in docs by @cjhbtn
'+ - '
Url
'+ - '

'+ - '
Http


'+ - '
Method
'+ - '

'+ - '
Header

'+ - '
Parameters


'+ - 'Submit

'+ - '
Response is also displayed in the browser console, if [Debug mode] is checked'+
-                        '
'); + var submitButton = $('Submit'); + submitButton.click(function () { + try { + $('#ApiTest form').submit(); + var url_root = $('#url_root').val().trim(); + if (url_root == '') + throw Error('url is empty'); + var http_method = $('#http_method').val().trim(); + var method = $('#method').val().trim(); + var headers = $('#headers').val().trim(); + var params = $('#params').val().trim(); + if (params == '') { + params = '{}'; + $('#params').text(params); + } + ApiWorker(http_method, url_root, method, JSON.parse(headers), JSON.parse(params), function (response) { + $('#response').html(JSON.stringify(response, null, 4)); + }, function (error) { + $('#response').text(error); + }); + } catch (e) { + $('#response').text(e.toString()); + } + }); + parent.append( + $('
').append( + 'Fork me on GitHub' + + 'Some documentation can be found in docs by @cjhbtn' + + '
Url
' + + '
Http

' + // The PATCH method is used to apply partial modifications to a resource. + '
Method
' + + '
Header

' + + '
Parameters