How to change user selected text in JavaScript - javascript

I would like to change the text that will be pasted without changing the selection's appearance on the screen, similar to what the Linux xsel command can do.
Ideally this would work on all platforms and browsers, but at the very least I want it to work on Chrome running on Linux.
My usecase is that I have a button that selects a block of shell commands from the web page (this is done by some library magic that I have no easy access to) and I want to add a terminating newline "behind the scenes" so that when the user middle-clicks in the console, he gets the text that appears highlighted on the screen plus a newline.
How to do it?

Related

How do I click a dialog button in MS Word on MacOS via Apple's Javascript for Automation?

I'm looking to click dialog boxes that appear in MS Word by using Apple's JXA automation tooling.
I'd like to click on the following dialog boxes:
Word found unreadable content in Do you want to recover the contents of this document? If you trust the source, click Yes
Word detected and repaired the following errors. To view each repair...
I'd like to click Yes on the first one, and close on the second one.
For context, I'm looking to extend the docx2pdf library in order to automatically skip these dialogs when converting a file to PDF. Right now, there are some manual clicks required due to these dialogs that I would like to fully automate.
What is the JXA code that I would need to automate this task?

Access link in browser through right click, Applescript and/or Javascript?

I want to right click a regular link in (primary) Chrome, Safari (secondary) or Firefox/Tor Browser (tertiary) and then send it somewhere else using Applescript. How do I do this?
I could imagine that I first use Javascript to get the actual link and then return it as text to an Applescript for further processing. Does it make sense? If so, I would appreciate some hints about how to get going.
You can do this by way of an Automator service, which will add a contextual (right-click) menu item when you click on a link in Safari (or Chrome).
Read the Mac Automation Scripting Guide: Making a Systemwide Service, which has a step-by-step guide on how to create an Automator service.
It will look something like this during construction:
This service, as you can see, is active only in Safari when there is a block of text selected. Unfortunately, you can't limit it to just hyperlinks, but it does include them.
If the service is available (i.e. you are in Safari and have selected some text), the menu item corresponding to the service appears in the right-click submenu called "Services", and it'll be named according to what you saved the workflow as in Automator. In my case, the service is named "Process URL".
When clicked, it executes the AppleScript you can see in the workflow (it does this in the background, invisibly).
This is where your suggestion of using JavaScript was a good one to employ: the AppleScript executes a bit of JavaScript in Safari (provided you have the right permissions enabled for it to do so, ticked in Safari's "Develop" menu).
This JavaScript returns one of two results:
▸ If the text selected is plain text with no hyperlink underneath it, it returns an empty string;
▸ If the text selected is hyperlink text, it returns the hyperlinked URL.
It stores this in the variable href, which you can then do whatever you want with.
The good thing about hyperlinks is that you don't have to click and drag to select the text (although you can if you wish, and only part of the text needs to be selected). Instead, you can just right-click on a link, and it automatically selects it for you, making it ideal to activate the service is this manner.

Enable text selection on a website using JavaScript

I know that typing
javascript:void(document.oncontextmenu=null) allows users to enable right click on a webpage
and javascript:void(document.onselectstart=null) should enable text selection if i'm correct?
but I tried typing these on http://www.pcworld.co.uk/gbuk/laptops-netbooks/laptops/apple-laptops/apple-macbook-pro-15-with-retina-display-21576115-pdt.html and it won't work.
by the way I typed it in the console of Google Chrome browser.
#Pointy pointed out that text selection worked fine for him. I tried in a few browsers - in Google Chrome specifically, on mouse down, the cursor changes when one tries to select text on a particular part of the page, and it does not allow this. However, on Firefox, you are perfectly capable of selecting the text.
For your intents, use a different browser and select the text.
For all we know, that sight could have custom javascript and logic preventing text selection, though its pretty lame that it doesn't work cross browser. But this custom logic is probably why your above mentioned commands aren't working.

Forcing keyboard to display when a text input is selected

The scenario, I have an input text field I'm doing partial auto-complete on (exactly like the Tags input field on Stack Overflow... because that's what is, basically).
As a user is typing, I suggest things they may typing; the user then clicks one the suggestions, it replaces a bit of the fields content, and places the cursor after where the insert occurred.
All this works, but after the insertion the software keyboard isn't shown anymore. You can replicate this on the desktop Stack Overflow site.
The code on SO isn't exactly the same, though the symptom is.
Here's the actual code called after the text is inserted into the text field.
$(field).focus()[0].setSelectionRange(newCaretPos, newCaretPos);
(Yes yes, setSelectionRange is non-standard; but it works on all the mobile devices I care about just this moment, so ignore it for now)
On android, iphone, etc. this works fine (the keyboard "flickers" but I'm willing to live with it.
So, the question basically boils down to: How can I force the onscreen keyboard to appear on Windows Phone 7 (Mango update)?
Sorry, there is no guaranteed way to force the SIP to display in WP7. :(
Normally setting the focus on the textbox would force the keyboard. Have you tried just setting the focus? (i.e. not setting the selection range)

ContentEditable element does not react on hotkeys [duplicate]

I'm developing a Chrome extension that injects JS code into all opened tabs. The task of this code includes displaying a textbox, implemented as a contentEditable <div> to allow formatting. However, the contentEditable element doesn't play well with some websites, e.g. Twitter.
Try it yourself:
Go to twitter.com
Paste javascript:document.body.setAttribute('contentEditable', true); in the address bar and hit enter.
Click anywhere in the document - you should now be able to edit the text.
Try typing a j or k. You will see: The character does not appear, instead, the selection is moved (a tweet in the list will be highlighted).
I have tried hard to find out how Twitter's keyboard navigation is implemented, but could not find any hints. Can you give me any tips on how to solve this problem (and avoid similar problems on other websites)?
Using an iFrame for the input triggers some other, very weird bugs, so I want to avoid that if it's possible.
Please make sure you don't have installed vim keyboard extension on google chrome.
J and K are then used for navigation shortcuts. (up/down).

Categories