September 2016: I stopped iOS development and removed all apps from the App Store.

JavaScript Actions

The JavaScript actions are for power users to write and execute scripts written in JavaScript. It can be useful for services that are currently not supported by Mr. Reader.

Attention: It uses the JavaScript engine, but it's without browser extensions or stuff like jQuery, AJAX, etc. If you want them, then please write and use a bookmarklet instead.

Example: Posting an article to Pinboard (uses Basic Auth)

Of course it is not the best example, because Pinboard is already supported by Mr. Reader. But it's good to see how it can be used.


username = 'your Pinboard username';
password = 'your Pinboard password';

article_url_encoded = '[URL]';
article_title_encoded = '[TITLE]';

url = 'https://api.pinboard.in/v1/posts/add';
url += '?url='+article_url_encoded;
url += '&description='+article_title_encoded;

var headers = new Object();
headers['Authorization'] = 'Basic '+base64encode(username+':'+password);

httpPost(url, headers, null, function(response, status, error) {
  if(status == 200) {
    doneAlert('Article posted to Pinboard!', decode(article_title_encoded));
  } else {
    alert(status + ':' + error + ' (' + response + ')');
  }
});

Example: Collect article URLs

In this example I collect article URLs and copy them formatted to the clipboard.

Example: HTTP POST data (bookmarking service)

In this example I show you how to do a HTTP POST request (form-urlencoded) and how to get some input from the user.

Adding a JavaScript action to the Sharing Menu

Open the Mr. Reader Settings, tap on 'Services', scroll to the bottom, and tap on add 'JavaScript'. Now enter the required data. Here is a description of the input fields:

NameGets used as title in the Sharing Menu.
CodeThe JavaScript code to be executed.
Standard MenuIf it should be shown in the standard Sharing Menu.
Text Selection MenuIf it should be shown in the Sharing Menu when some text is selected.
Link MenuIf it should be shown when you opened the Sharing Menu on a link.

When you edit the 'Code' field you will see a keyboard extension with the tags that can be used/inserted:

[SOURCE]Gets replaced with the feed name
[AUTHOR]Gets replaced with the article author
[TITLE]Gets replaced with the article title
[TEXT]Gets replaced with the article content or selected text
[TEXT-SELECTED]Gets replaced with the selected text
[URL]Gets replaced with the URL of the article
[URL-SHORTENED]Gets replaced with the shortened URL. It uses the current set URL shortener from 'Copy Short URL', Twitter, Buffer, ...

Hint: The values of those tags are always inserted URL encoded!

Additional JavaScript functions (Mr. Reader specific)

Alerts & Logging

errorAlert(title, subtitle)

Shows my red alert at the top of the screen with a title and a subtitle. Could be used when something goes wrong.


errorAlert('Post to Pinboard failed:', errorMessage);
doneAlert(title, subtitle)

Shows my green alert at the top of the screen with a title and a subtitle. Could be used when something was done successful.


doneAlert('Article posted to Pinboard.', '');
infoAlert(title, subtitle)

Shows my blue alert at the top of the screen with a title and a subtitle.


infoAlert('This is the title', 'This is the subtitle');
alert(message)

Shows the given message in a standard iOS alert with an OK button.


alert('This is an alert!');
consoleLog(message)

Logs the given message parameter to the iOS log file. Only useful when you have access it.


consoleLog('This message will be written to the iOS log file.');

3rd Party Apps

openApp(url)

Opens an app by using the given url.


app_url = 'onenote://';
open(app_url);

HTTP Calls

httpGet(url, headerValues, callbackFunction)

Makes an HTTP GET request for the given url, sets the given header values and calls the given JavaScript function afterwards.


var headers = new Object();
headers['Accept-Encoding'] = 'gzip, deflate';

httpGet('http://ip.jsontest.com/', headers, function(response, status, error) {
  json = asJsonObject(response);
  ipaddress = json["ip"];
  copyStringToClipboard(ipaddress);
  alert(ipaddress);
});
httpPost(url, headerValues, body, callbackFunction)

Makes an HTTP POST request for the given url, sets the given header values, sets the body as given and calls the given JavaScript function afterwards.


var headers = new Object();
headers['Content-Type'] = 'application/json; charset=utf-8';
headers['User-Agent'] = 'Mr. Readers JavaScript action';

postBody = '{"ip": "85.182.15.162", "locale": "de"}';

httpPost('https://posttestserver.com/post.php', headers, postBody, function(response, status, error) {
  alert(status+':'+response);
});

User Input

inputPrompt(title, message, callbackFunction)

Let the user enter a single value.


inputPrompt('Auth Token', 'Please enter your auth token.', function(userInput, cancelled) {
  if(cancelled) {
    consoleLog('Cancelled by user');
  } else {
    consoleLog('Input:' + userInput);
}
});
loginAndPasswordPrompt(title, message, callbackFunction)

Let the user enter a login and password.


loginAndPasswordPrompt('Login to xyz', 'Please enter your credentials.', function(login, password, cancelled) {
  if(cancelled) {
    consoleLog('Login cancelled by user');
  } else {
    consoleLog('Login:' + login + ', password:' + password);
  }
});
confirmationPrompt(title, message, callbackFunction)

Confirmation dialog with a 'Cancel' and a 'OK' button.


confirmationPrompt('Really?', 'Please confirm it.', function(cancelled) {
  if(cancelled) {
    consoleLog('Cancel button pressed');
  } else {
    consoleLog('OK button pressed');
  }
});
yesAndNoPrompt(title, message, callbackFunction)

Confirmation dialog with a 'Yes' and a 'No' button.


yesAndNoPrompt('Delete clipboard afterwards?', 'Please confirm it.', function(disallowed) {
  if(disallowed) {
    consoleLog('No button pressed');
  } else {
    consoleLog('Yes button pressed');
  }
});

Clipboard

copyStringToClipboard(value)

Copies the given value to the iOS clipboard.


url = 'http://www.curioustimes.de/mrreader/';
copyStringToClipboard(url);
stringFromClipboard()

Gets the current text content from the iOS clipboard.


clipboard_content = stringFromClipboard();

Settings

existsSetting(key)

Checks if a setting for the given key exists.


if(existsSetting('MyKey')) {
  consoleLog('My setting exists');
} else {
  consoleLog('My setting does not exist');
}
getSetting(key)

Get the string value for the given key from the settings.


settingsValue = getSetting('MyKey');
consoleLog('settings value:' + settingsValue);
setSetting(key, value)

Set the given string value for the given key into the settings.


setSetting('MyKey', 'my value');
removeSetting(key)

Removes the setting of the given key.


removeSetting('MyKey');
settingKeys()

Get an array of all setting keys.


keys = settingKeys();
keys.forEach( function(key) {
  removeSetting(key);
  consoleLog('Setting deleted:' + key);
});

JSON

asJsonObject(value)

Converts the given string value to a JSON object which can be access in a common way afterwards.


json_string = '{"ip": "85.182.15.162"}';
json = asJsonObject(json_string);
ip_address = json["ip"];
// ip_address will be: '85.182.15.162'
asJsonString(value)

Converts the given object to a JSON string.


var json = new Object();
json['ip'] = '85.182.15.162';
json['locale'] = 'de';

json_string = asJsonString(json);
// json_string will be: {"ip": "85.182.15.162", "locale": "de"}

Conversion

encode(value)

My URL encoding function.


encoded = encode('hello world');
// encoded will be: 'hello%20world'
decode(value)

My URL decoding function.


decoded = decode('hello%20world');
// decoded will be: 'hello world'
base64encode(value)

Encodes the given string to base64. Useful to create for example the HTTP basic auth value.


base64_encoded = base64encode('username:password');
// base64_encoded will be: 'dXNlcm5hbWU6cGFzc3dvcmQ='
base64decode(value)

Decodes the given base64 string.


base64_decoded = base64decode('dXNlcm5hbWU6cGFzc3dvcmQ=');
// base64_decoded will be: 'username:password'
md5(value)

Creates a md5 hash from the given string value.


md5 = md5('hello world');
// md5 will be: '5EB63BBBE01EEED093CB22BB8F5ACDC3'