How can I initialize a link from all text in input - javascript

I have only one input (type=text), in this input someone will type his text and he can paste a link in this field also.
What I should to do? I should initialize this link in text for beautiful link preview(for this I'll use link-preview-js).
So, please, help me to initialize a link
Using: react, reactTS
Input:
The URL() constructor returns a newly created URL object representing the URL defined by the parameters. If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown.
www.gfokgfkl.com (and preview under)
Expect some methods or special type like File -> FileReader for(for ex) images.

You might want to use a regex. This answer has a good regex to match links. And then you can use String.prototype.match to extract the link from your input.

Related

How to store variable and set the variable in field

I'm using Selenium 3.17.0.
I want to type a text from a javascript, but it doesn't work!
I have tried this (this types the text "undefined")
and this (this types the entire script as text)
but nothing works! maybe I'm doing wrong the javascript but I don't know, I'm new in all of this, please help!
btw this is my first post, sorry if I'm doing it wrong.
If I understand your question correctly, you are trying to generate a random string using javascript and then store the value in a variable. Then enter that variable value using type.
you have to use execute script command to run the javascript and target (your javascript) should be return "free text" + Math.floor(Math.random()*100) as shown in the below screenshot.

Anyway to know it is stored in savedsearch or not

I'd like to search using savedsearch.
Here my code snippet goes.
var searchresults = nlapiSearchRecord('item', search_id, null, null);
search_id is defined as parameter in text field.
This is suitelet script so if you couldn't find similar search_id in savedsearch then it throws exception.
To avoid this I'd like to check if there is any similar internal id in saved searches.
For instance if there are two saved searches which ids are customsearch1, customsearch2.
If search_id is 'cust' then it throws exception and script finished with error.
It shows this in script log
'That search or mass update does not exist.'
Looking forward to hearing from you soon.
Regard
You can do a saved search of saved searches. You could take the results and use regex to determine if there is a similiar one. Use trim plus regex.
You could prevent this by changing your search_id parameter to a List/Record of Saved Searches.
Any reason why it has to be a text field?

Regex replacement with prompting/callback UI

I'm trying to write a function that takes a long string of text, identifies place holders within the text, and prompts the user to supply a value that should take the place of the placeholder. The markup for the placeholders looks similar to markdown used for images or links:
some text, some more text, ?[name][description] more text, not just commas
Where name and description are arbitrary runs of text. When I've found these placeholders, I want to pop up a nicely formatted dialog, using the names and descriptions, and have the user supply a replacement value.
I already have a nice function (called htmlPrompt) available where you hand it a piece of HTML (for the main part of the prompt), has a text box, and then calls a callback function you've supplied with the result (or null if Cancel is pressed), with the following signature:
function (htmlText, inputStartValue, callback)
Before plugging in this function, I wrote the rough and ready:
myText = myText.replace(/(\?\[(.+)\][ ]?(?:\n[ ]*)?\[(.+)\])/g,
function (wholematch, m1, m2, m3) {
var repValue = prompt(m2);
if (repValue == null)
{
return m1;
}
return repValue;
});
Which uses the DOM built-in prompt method - which doesn't really do an adequate job for me, when it comes to formatting.
However, I can't think of a way of plugging in htmlPrompt - it only simulates a modal dialog and provides the final result by calling callback.
I did think of trying to manually do the replacements, using the results from match rather than replace - but so far as I can see, the values returned by match are just strings - they don't give you anything useful (such as the location of the match within the overall text).
Or do you think I'm going about this completely wrong? The overall flow I want is:
Find each placeholder in the text
Prompt the user for a replacement, using both the name and description values
Replace the placeholder expressions in the text with the user supplied value.
For each of the name and description tupples:
First use match to read name and desription.
Prompt user.
Then use replace to replace those.

How do I auto-populate a text field from URL Parameter using Javascript

This page (http://forums.iis.net/t/1153336.aspx) explains how to do exactly what I am wanting to do, except due to the limitations of our system I only get to enter javascript to pull this information into the textbox. Can someone explain how I can use a parameter in a URL to auto fill a textbox?
You can use location.search to access the url query and inject it into your text value
If your url is /page.htm?x=y, you can use the following code to set the value of a text field. This post tells you how to grab values from the query string
// See the link above for getUrlParams
var params = getUrlParams();
document.getElementById('myTextFieldId').value = params.x;
Notice that the example you've linked to suffers from a http://en.wikipedia.org/wiki/Cross-site_scripting vulnerability. To avoid that, you must escape the HTML before you output it on the screen
You should be able to do something similar using windows.location in js
http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
You can easily use the answer of this question to get parameters from the URL using JavaScript. Then set the value of your textboxes like this:
this.form.elements["element_name"].value = 'Some Value';
If you want a quick solution, you might be interested in using this code snippet (inspiration taken from here, but modified by myself):
function getURLParameter(e) {
return decodeURI((new RegExp(e + "=(.+?)(&|$)").exec(location.search) || [, ""])[1]);
}
if (getURLParameter("inputKey") === "") {
console.log("No URL param value found.");
} else {
document.getElementById("inputBox").value = getURLParameter("inputkey");
}
Let's say that your url is example.com?inputKey=hello world. If so, using the above code will fill in the input with the ID of "inputBox" with the phrase "Hello world". If the URL was just example.com, it wouldn't prefill the box, and instead just act as though there was nothing (show the placeholder etc).

passing hashtags as parameters to twitter

I have the following tweet:
var tweet = "I might actually do a 5K: http://t.co/tXQIYlUt #zombies #running"
And I would like to pass this to the twitter api using js
$('.my_div').append('Tweet')
My JS creates this: https://twitter.com/share?text=I%20might%20actually%20do%20a%205K:%20http://t.co/tXQIYlUt%20#zombies #running&via=JustinZollars&url=
which renders this way at twitter.com:
I might actually do a 5K: http://t.co/tXQIYlUt http://mydomain.com/
notice it cut out my hash tags. how can I sanitize my url?
Resources:
docs
encodeURIComponent() your GET param (the tweet variable). Also, don't encode the GET params that you do want to have special meaning (the & and =).
jsFiddle.

Categories

Resources