Can JavaScript Bookmarklets Modify Text Selections and Clipboard Contents?

  • Context: Java 
  • Thread starter Thread starter granpa
  • Start date Start date
  • Tags Tags
    Javascript
Click For Summary
SUMMARY

This discussion focuses on the capability of JavaScript bookmarklets to manipulate text selections and clipboard contents. Users inquire about creating bookmarklets that can insert Unicode characters directly at the cursor position and modify clipboard contents by appending selected text. A specific example provided demonstrates a bookmarklet that replaces selected text with its ROT13 equivalent using JavaScript functions to traverse and modify text nodes in the DOM.

PREREQUISITES
  • Basic understanding of JavaScript programming
  • Familiarity with the Document Object Model (DOM)
  • Knowledge of clipboard API usage in web applications
  • Experience with creating and using bookmarklets in web browsers
NEXT STEPS
  • Learn how to use the Clipboard API in JavaScript
  • Research methods for inserting text at the cursor position in web applications
  • Explore advanced JavaScript functions for text manipulation in the DOM
  • Investigate the creation and deployment of bookmarklets for various use cases
USEFUL FOR

Web developers, JavaScript enthusiasts, and anyone interested in enhancing user interaction through bookmarklets and clipboard manipulation techniques.

granpa
Messages
2,268
Reaction score
7
can one write a javascript bookmark that will print a single unicode character (like a smiley face or a math symbol) to the clipboard (or better still straight to the cursor position)?

how about print something like (SUB) to the clipboard then append what is highlighted to the clipboard then append (/SUB) to it then paste the clipboard to the current text area?
 
Last edited:
Technology news on Phys.org
http://www.sitepoint.com/article/bookmarklets/

Replaces each letter in the selected text with its rot13 equivalent

javascript:var coding = "abcdefghijklmnopqrstuvwxyzabcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"; function rot13(t) { for (var r = "",i=0;i<t.length;i++) { character = t.charAt(i); position = coding.indexOf(character); if (position > -1) character = coding.charAt(position + 13); r += character; } return r; } S=window.getSelection(); function t(N) { return N.nodeType == N.TEXT_NODE; } function r(N) { if (t(N)) N.data = rot13(N.data); } for (j=0;j<S.rangeCount;++j) { var g=S.getRangeAt(j), e=g.startContainer, f=g.endContainer, E=g.startOffset, F=g.endOffset, m=(e==f); if(!m||!t(e)) { /* rot13 each text node between e and f, not including e and f. */ q=document.createTreeWalker(g.commonAncestorContainer, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, null, false); q.currentNode=e; for(N=q.nextNode(); N && N != f; N = q.nextNode()) r(N); } if (t(f)) f.splitText(F); if (!m) r(f); if (t(e)) { r(k=e.splitText(E)); if(m)f=k; e=k;} if (t(f)) g.setEnd(f,f.data.length); } void 0
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 17 ·
Replies
17
Views
320K
  • Sticky
  • · Replies 2 ·
Replies
2
Views
506K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K