Skip to content

Instantly share code, notes, and snippets.

Last active July 4, 2020 23:22
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 blacklight/d80c571705215924abc06a80994fd5f4 to your computer and use it in GitHub Desktop.
Save blacklight/d80c571705215924abc06a80994fd5f4 to your computer and use it in GitHub Desktop.
A sample script showing the capabilities of the Platypush web extension web scripts API
* This script shows some of the capabilities of the Platypush Script API, and how
* you can leverage the provided API to create custom scripts that can be executed
* from anywhere in your browser.
* In order to create a new script:
* 1. Make sure that you have added at least one Platypush device in the extension
* configuration.
* 2. Select the device from the menu -> Run Action -> select "Script" on "Action mode".
* 3. Create your script, test it, and when you're happy save it. It will be listed
* among the available actions and you can run it on any tab either from the toolbar
* menu or from any context menu.
* 4. Note: some of the APIs (like those used to access the DOM or the current
* tab) won't be available in the context of the extension (i.e. when you test
* your script on the extension page) due to browser sandboxing limitations.
export default {
example: async (app, args) => {
// Show the available arguments
console.log('Arguments', args);
/* Output:
* args = {
* // This is the Platypush host selected in the context
* host: {
* // Host name
* name: "your_host",
* // Host IP/DNS name
* address: "",
* // HTTP port
* port: 8008,
* // Websocket port
* websocketPort: 8009,
* // Uses SSL
* ssl: false,
* // Access token:
* token: "your_token"
* },
* // Set if the action is executed from the context menu
* tabId: <selectedTabId>,
* // Set if the action is executed from the context menu on a page element
* target: "<target html>"
* }
// Show the available API
console.log('API', app);
// Run a Platypush action on the selected host (example: play/pause the music)
const response = await{ action: 'music.mpd.pause', args: {} },;
console.log('Response from Platypush server', response);
// Get the URL in the current tab
const url = await app.getURL();
console.log('URL', url);
// Set the current page URL (i.e. change the current page)
// Open a URL in a new tab
// The app API also exposes the axios library for performing AJAX calls
const apiResponse = await app.axios.get('');
console.log('API response', apiResponse);
// Get the DOM object of the current page
const dom = await app.getDOM();
// You can perform any transformations on the DOM. For instance, the API
// also exposes the Mercury Parser API for simplifying/distlling text from
// HTML. You can use it to simplify the content of any page.
const simplifiedDOM = await app.mercury.parse(url, dom.body.innerHTML);
// Finally, you can use setDOM to replace the content of the page
await app.setDOM(`<html><head>${simplifiedDOM.title}</head><body>${simplifiedDOM.content}</body></html>`);
// If the user spawned this action from a context menu after clicking on
// an element then you can grab the target element
if ( {
console.log('Selected element (HTML)',;
// Convert HTML to DOM
const targetDOM = app.HTML2DOM(;
console.log('Selected element (DOM)', targetDOM);
// Anything returned from the function will be returned to the called
return 42;
// vim:sw=2:ts=2:et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment