Monthly Archiv: October, 2016

Simple HTML DOM (New)

Package:
Simple HTML DOM
Summary:
Manipulate HTML elements using DOMDocument
Groups:
HTML, Parsers, PHP 5
Author:
Lars Moelleken
Description:
This class can manipulate HTML elements using DOMDocument

This is a fork of SimpleHTMLDOM package that uses DOMDocument classes instead of HTML string manipulation...

Read more at http://www.phpclasses.org/package/9954-PHP-Manipulate-HTML-elements-using-DOMDocument.html

PHP Associative Array Key Case Insensitive (New)

Package:
PHP Associative Array Key Case Insensitive
Summary:
Store associative array with case insensitive keys
Groups:
Data types, PHP 5
Author:
Christian Vigh
Description:
This class can store associative array with case insensitive keys...

Read more at http://www.phpclasses.org/package/9904-PHP-Store-associative-array-with-case-insensitive-keys.html

PHP MySQL Websocket Chat

Package:
PHP MySQL Websocket Chat
Summary:
Websocket chat that stores messages in MySQL
Groups:
Chat, Databases, HTTP, PHP 5
Author:
Johnny Mast
Description:
This package can Websocket chat that stores messages in MySQL using PDO...

Read more at http://www.phpclasses.org/package/9947-PHP-Websocket-chat-that-stores-messages-in-MySQL.html#2016-10-11-10:21:02

Quick Tip: Accessing The Clipboard With JavaScript

accessing-the-clipboard-with-js_

In this article we’re going to show you how to use simple vanilla JavaScript snippets to:

  1. Add text to the clipboard on user action, such as the press of a button.
  2. Modify the content of the clipboard when a user copies something.

The APIs we will be using don’t require any external libraries, and have almost perfect browser compatibility!

Copy On Click

An awesome accessibility feature you can add to your website is the ability to copy strings directly via button press. This interaction can be applied to quickly grab URLs, long strings such as SSH keys, terminal commands, hex colors, or any other data that is frequently copy & pasted.

To make this happen we will need to use a cool JavaScript method called execCommand(). It allows us to invoke a number of different events that manipulate editable content such as making text bold/italic, doing undo/redo, and also copy/cut/paste.

document.execCommand('copy');

This works exactly like pressing CTRL/Cmd+C on your keyboard, so in order to copy any text we first need to have it selected. This is possible in JavaScript thanks to the Selection API, which allows us to programatically make a text selection from any HTML element on the page.

var button = document.getElementById("copy-button"),
    contentHolder = document.getElementById("content-holder");

button.addEventListener("click", function() {

    // We will need a range object and a selection.
    var range = document.createRange(),
        selection = window.getSelection();

    // Clear selection from any previous data.
    selection.removeAllRanges();

    // Make the range select the entire content of the contentHolder paragraph.
    range.selectNodeContents(contentHolder);

    // Add that range to the selection.
    selection.addRange(range);

    // Copy the selection to clipboard.
    document.execCommand('copy');

    // Clear selection if you want to.
    selection.removeAllRanges();

}, false);

To see the example in action check out the editor below:

(Play with our code editor on Tutorialzine.com)

In the example above the content we want to copy is simply stored in a paragraph. If the text you need is not on the page, you will need to first write it in an element hidden off-screen.

Modify Copied Text

Here we will show you how to manipulate content in the clipboard after it’s been copied. This can be very useful for escaping code, formatting numbers and dates, or for other text transformations such as uppercase, lowercase, etc.

JavaScript provides us with copy() and paste() events, but they are designed in such a way that the content stored in the clipboard is secure:

  • In the copy event handler we cannot read what’s stored in clipboard, as there may be personal info which we shouldn’t have access to. We can, however, overwrite the clipboard data.
  • In the paste event it’s the opposite: we can read the data, but we cannot change it.

Since we want to read and write at the same time, we will need to use the Selection API once more. Here is the solution:

document.addEventListener('copy', function(e){

    // We need to prevent the default copy functionality,
    // otherwise it would just copy the selection as usual.
    e.preventDefault();

    // The copy event doesn't give us access to the clipboard data,
    // so we need to get the user selection via the Selection API.
    var selection = window.getSelection().toString();

    // Transform the selection in any way we want.
    // In this example we will escape HTML code.
    var escaped = escapeHTML(selection);

    // Place the transformed text in the clipboard. 
    e.clipboardData.setData('text/plain', escaped);

});

You can try the code in the editor below:

(Play with our code editor on Tutorialzine.com)

Further Reading

In this quick tip we presented you two useful snippets for working with the clipboard in pure vanilla JavaScript. We used a bunch of hip native APIs, so here they are again if you want to read more about them:

  • execCommand – Execute actions such as copy, paste, cut, bold, italic, underline, delete, and many others. – MDN, Can I Use
  • Selection API – Allows developers to make a range selection from any text on the page. – MDN, Can I Use
  • JavaScript Copy Event – An event fired when users press CTRL/Cmd+C or choose “copy” from the right-click menu. – MDN, Can I Use

Also, if you need more control over copy/paste/cut events, you can use a library such as clipobard.js. It has lots of features and provides a nice clean API for managing the clipboard.

We hope you enjoyed this article! Feel free to ask question or leave suggestions in the comment section below :)

Powered by Gewgley