Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vigorouscoding
Created August 15, 2016 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigorouscoding/352a8225a2ef549a98b261e2e148ecd5 to your computer and use it in GitHub Desktop.
Save vigorouscoding/352a8225a2ef549a98b261e2e148ecd5 to your computer and use it in GitHub Desktop.
Bookmarklet (seethis article https://support.mozilla.org/en-US/kb/bookmarklets-perform-common-web-page-tasks for more infos on bookmarklets) for Structr to have a fast way to perform some tasks via the REST interface.
javascript:
/**
* Structr-UI Helpers - a couple of simple functions to send REST requests to Structr.
* Calls to Structr automatically get the header flag 'Structr-Websocket-Broadcast' set to 'disabled' so that
* performance does not suffer too much when doing larger tasks.
*/
function structr_rest(method, path, data, callback) {
var allow = true;
if (!method || method == '') {
allow = false;
console.log('method can not be empty');
}
if (!path || path == '') {
allow = false;
console.log('path can not be empty');
}
var ajax_options = {
method: method,
url: '/structr/rest/' + path
};
if (method == 'POST' || method == 'PUT') {
if (typeof data == 'string') {
try {
JSON.parse(data);
} catch (syntaxerrors) {
allow = false;
console.log('Provided data is not a JSON string: ', data);
}
} else if (typeof data == 'object') {
data = JSON.stringify(data);
} else {
allow = false;
console.log('data must be either a JSON string or an object');
}
ajax_options['data'] = data;
}
ajax_options.headers = {
'Structr-Websocket-Broadcast': 'disabled'
};
if (allow) {
$.ajax(ajax_options).success(function(data, status, xhr) {
console.log(status, data);
if (callback) {
if (data.result.length != null) {
data.result.forEach(function(res) {
callback(res);
});
} else {
callback(data.result);
}
}
}).error(function(xhr, status, error) {
console.log(xhr, status, error);
});
}
}
function structr_get(path, callback) {
structr_rest('GET', path, {}, callback);
}
function structr_head(path) {
structr_rest('HEAD', path);
}
function structr_put(path, data) {
structr_rest('PUT', path, data);
}
function structr_post(path, data) {
structr_rest('POST', path, data);
}
function structr_delete(path) {
structr_rest('DELETE', path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment