JavaScript Injectors - screenly/playground

Screenly is an open-source digital signage solution that supports playing web content. JavaScript Injectors in Screenly allow you to interact with web applications displayed on Screenly, enabling dynamic and engaging content. This guide explains the concept of JavaScript Injectors and provides examples of possible options using the provided documentation and code snippets.

JavaScript Injectors: The Big Picture

JavaScript Injectors are a feature in Screenly that lets you execute custom JavaScript code on web pages displayed on the digital signage. This capability can be used to manipulate the content, customize the layout, or integrate with external services.

Design Philosophy

Screenly’s design philosophy emphasizes simplicity, modularity, and ease of use. JavaScript Injectors follow this philosophy by providing a clean and straightforward interface for adding custom JavaScript code to web pages.

Programming Languages

Screenly primarily uses JavaScript, HTML, and CSS for creating interactive and dynamic digital signage content. The JavaScript Injectors feature specifically focuses on JavaScript.

Screenly API

Screenly provides an API for managing assets, playlists, and settings. JavaScript Injectors can interact with the Screenly API to fetch or update data, making your digital signage content more dynamic.

Key Technologies and Dependencies

JavaScript Injectors rely on the core JavaScript features and some popular libraries like jQuery. Familiarity with these technologies will help you make the most of JavaScript Injectors.

Understanding JavaScript Injectors

JavaScript Injectors in Screenly allow you to execute custom JavaScript code on web pages displayed on the digital signage. This capability can be used to manipulate the content, customize the layout, or integrate with external services.

To use JavaScript Injectors, you need to provide a JavaScript file containing the custom code you want to execute. This file will be loaded and executed on the targeted web pages.

Example: Basic JavaScript Injector

Create a JavaScript file named custom-script.js with the following content:

console.log('Custom script executed!');

Upload this file to Screenly as an asset, and then include it in the web page’s HTML using a script tag:

<script src="custom-script.js"></script>

When the web page is displayed on the digital signage, the custom script will be executed, and you will see the message “Custom script executed!” in the Screenly logs.

Possible Options and Examples

JavaScript Injectors support various options for interacting with web applications displayed on Screenly. Here are some examples of what you can do with JavaScript Injectors.

Modifying Page Content

You can use JavaScript Injectors to modify the content of a web page. For example, you can change the text, hide or show elements, or update data.

Example: Changing Text

Create a JavaScript file named change-text.js with the following content:

const heading = document.querySelector('h1');
heading.textContent = 'Updated heading!';

Upload this file to Screenly as an asset, and then include it in the web page’s HTML using a script tag:

<script src="change-text.js"></script>

When the web page is displayed on the digital signage, the heading text will be updated to “Updated heading!”.

Interacting with External Services

JavaScript Injectors can interact with external services, such as APIs, to fetch or update data. This capability can be used to display real-time information on your digital signage.

Example: Displaying Weather Data

Create a JavaScript file named weather-data.js with the following content:

fetch('https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY')
.then(response => response.json())
.then(data => {
const temp = document.createElement('p');
temp.textContent = `Temperature: ${data.main.temp} K`;
document.body.appendChild(temp);
});

Replace YOUR_API_KEY with your actual OpenWeatherMap API key. Upload this file to Screenly as an asset, and then include it in the web page’s HTML using a script tag:

<script src="weather-data.js"></script>

When the web page is displayed on the digital signage, the current temperature in London will be fetched and displayed on the page.

Using Libraries and Frameworks

JavaScript Injectors support using popular libraries and frameworks, such as jQuery, Vue.js, or Angular. This capability enables you to create more complex and interactive digital signage content.

Example: Using jQuery

Create a JavaScript file named jquery-example.js with the following content:

$(document).ready(function () {
$('p').css('color', 'red');
});

Upload this file to Screenly as an asset, and then include it in the web page’s HTML using a script tag:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="jquery-example.js"></script>

When the web page is displayed on the digital signage, all paragraphs will have their text color changed to red.

Conclusion

JavaScript Injectors in Screenly offer a powerful way to interact with web applications displayed on digital signage. By understanding the concept and exploring the possible options, you can create dynamic, engaging, and customized content for your digital signage projects.

For more information, refer to the following resources: