How to use bookmarklet to open multi-links in clipboard? - javascript

In my notepad there are three links:
https://www.google.com https://www.bing.com https://www.yahoo.com
How to onekey open multi-links in clipboard?
i know that there are a lot of extensions to make it happen.
Is there a "bookmarklet" way?
Thanks in advance.
Is it possiable to use regex in bookmarklet?

Sure, you can use a regex if you need to (as in the code). In fact anything that executes in console can be re-worked as a bookmarklet.
Do you know how to make a bookmarklet? You can just save any page as a bookmark in your browser, and then replace its URL for the code in a specific format.
As for the question, I suppose that you wish to copy the links from a notepad, click the bookmarklet, paste the links, press enter and get all the links opened. Here is the solution:
I). Whitespace as a delimiter
1). To open links you can save this bookmarklet:
javascript:(function(){
/*prompt asks for the links delimited by whitespaces*/
var linksWithDelim = window.prompt('Insert links, with delimiters');
/*getting rid of extra spaces, with simple regex in replace*/
linksWithDelim = linksWithDelim.trim().replace(/ +/g, ' ');
var arrOfLinks = [];
/*links from prompt go to the array splited by the delimiter*/
arrOfLinks = linksWithDelim.split(' ');
/*the loop opens each link from the array*/
for (i=0; i<arrOfLinks.length; i++){
window.open(arrOfLinks[i]);
}
})();
2). As an input you can use your
https://www.google.com https://www.bing.com https://www.yahoo.com
Make sure the links have proper protocol like https:// and proper whitespaces.
II). New line as a delimiter
If your notepad contains the links delimited with new line breaks instead of whitespaces, you might replace the correspoding line of the code for
arrOfLinks = linksWithDelim.split('\n');
After changing that line the input like
https://www.google.com
https://www.bing.com
https://www.yahoo.com
copy-pasted to the prompt is supposed to work as well.

Related

How to grab URLs in JavaScript without harming embedded objects and inline URL

I wrote a RegExp to grab and encode URLs in JavaScript.
This works fine but, it introduced a bug into my app.
I have a span Element which is used to display Emojis like this:
<span style="background:url(http://localhost/res/emo/face/E004.png)"></span>
Now, I'm using this Regular Expression to grab and convert anything URL into actual HTML clickable links:
/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig
This ended up encoding the emoji URL into a clickable link.
Can anyone adjust that Code to Ignore URLs inside Elements or embedded Objects???
Please I need help!
This is the code:
var urlRegex = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig;
return txt.replace(urlRegex, function (url) {
var hyperlink = url;
if(!hyperlink.match('^https?:\/\/')) {
hyperlink = 'http://' + hyperlink;
}
return `${url}`;
});
I don't that the URLS inside
<span style="background:url(http://localhost/res/emo/face/E004.png)"></span>
were touched.
You would need to use negative look behind, which has limited support in JavaScript. (see here https://stackoverflow.com/a/50434875/6853740)
Simply adding negative look behind to your existing regex still doesn't work as expected:
((?<!url\()(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?) still matches "E004.png" in your example. Even other URL regexs from this post (What is the best regular expression to check if a string is a valid URL?) also match that. You may need to consider only looking for links that start with http:// or https:// which may help you recraft a regex that will only match full URLs.

How to replace the forward slash in a javascript bookmarklet

I am not a programmer, but know a little here and there. This is a bookmarklet I have in my browser. It is supposed to take the url of the page I am on, and when clicked, takes me to another site (example.com), and pass this first site into the url of the second site (e.g. sitechecker.com).
Problem is, the trailing slash on the example.com/ prevents sitechecker from working, so i need to get rid of the trailing slash somehow when its passed to the other site.
E.g.
No Good
http://www.example.com/
Good
http://www.example.com
Bookmarklet code:
javascript:(function(){ var url=location.href; var url=url.replace(/^(http|https):\/\//i,''); window.open('https://www.widgetfactory.com/index.html/all//'+encodeURIComponent(url)+'/Oc?l=us')})();
Try the following regex url.replace(/\/$/, ""); Exm. below
var url = 'http://www.example.com/';
console.log(url.replace(/\/$/, ""));

javascript fetch last url without prefix

I am looking to detect last url from text using javascript or mootools. Url canbe without prefix/scheme
I am working on URL auto sense like Facebook. Where a user may give an URL www.example.com or with http://www.example.com either of them should be detected by JavaScript. see stackoverflow detected URL that included with scheme without URL scheme it couldn't detect URL. In my case I need both.
Here is some text
'http://www.example.com www.example2.com'
Now I want www.example2.com It will be better if I get full array containing both http://www.example.com and www.example2.com
I searched a lot but couldn't find solution.
Most close to my requirements were Question about URL Validation with Regex and How do I extract a URL from plain text using jQuery?
Any help greatly appreciated.
by combing info in these 2 links:
How do I extract a URL from plain text using jQuery?
Detect URLs in text with JavaScript
We can get this:
http://jsfiddle.net/qQwGA/1/
If I understand what you're trying to do, this should cover it.
Given your input string, I think you just want to split it using spaces as separator?
.split(' ') ?
REGEX
/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi
**SAMPLE CODE**
var str = 'http://www.example.com www.example2.com scheme://username:password#domain:port/path?query_string#fragment_id';
var t = str.match(/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi);
/*
t contains :
[
"http://www.example.com",
"www.example2.com",
"scheme://username:password#domain:port/path?query_string#fragment_id"
]
*/
**DEMO**
>http://jsfiddle.net/wvYTd/
**DISCUSSION**
This regex will find any substring that looks like an URL in an input string.
No validation is performed on any URL found. For instance, if the input string is 3aBadScheme://hostname, the regex will detect it as an URL. In this example, 3aBadScheme is invalid since a scheme MUST start with a letter.
Excerpt from RFC3986
(...)Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-").(...)

How do I prevent spaces from being URL encoded for a `javascript:` URL?

I'm trying to use the following as a URL that executes javascript:
javascript:var field = document.getElementsByName("actions[hide]"); + for (i = 0; i < field.length; i++)field[i].click();
However, the spaces get URL encoded when I bookmark it, replaced with %20, which (for a reason unknown to me) causes the JS code not to work.
javascript:var%20field%20=%20unescape%20document.getElementsByName("actions[hide]");%20+%20for%20(i%20=%200;%20i%20<%20field.length;%20i++)field[i].click();
if you want to create a bookmarklet i would suggest you this site:
http://benalman.com/code/test/jquery-run-code-bookmarklet/
there is written that it is used for jquery code but you can also convert normal javascript with this generator. Or you can simplify use the jquery and convert your code from:
var field = document.getElementsByName("actions[hide]"); + for (i = 0; i < field.length; i++)field[i].click();
to
$('[name="actions[hide]"]').each(function() { $(this).click(); });
i use this script every time i create a new bookmarklet and i love it
EDIT: when you enter your code you must paste it without the "javascript:" text in front.
I didn't understand as well the purpose you mean for that javascript as url scheme.
Anyway if you put that string into the browse address bar to work within the current web page, probably it won't.
You can try to call an anonymous function:
Click me
Encoding spaces in javascript: URIs shouldn't (and doesn't in my experience) break scripts. The problem is most likely your + which is also a special character in URIs (it also means a space) but isn't being automatically converted by the browser as the character is allowed at that point in a URI.
You need to encode the + character as %2B (along with any other special characters you might have in the JS).

REGEX Str Replace- finding link text and making them links

I've written this functionality in Flash before without issue; however, I'm now attempting to do it with JavaScript and I'm running into some difficulty.
Using Regex, I'm trying to scour a string looking for anything that resembles a link... for example http://www.google.com or http://stacoverflow.com/question/ask; then wrap that result with the appropriate: :
<script type="text/javascript>
var mystring = "I'm trying to make this link http://facebook.com/lenfontes active."
/// REGEX that worked in Flash to grab ALL parts of the URL including after the .com...
var http = /\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/gi;
// preform the replace based on the Regex
mystring = mystring.replace(http, function(){
var link = arguments[0];
if (link.indexOf("http") == -1){
link = "http://" + link;}
return "<a href='"+link+"'>"+arguments[0]+"</a>";
});
$('#results).html(mystring);
</script>
The issue I'm having: anything after the ...com/ is ignored. Therefore the link isn't correct...
Even while I post this question, the Stack Overflow interface has taken my text and rendered it out with the appropriate link tags... I need to do that.
Any suggestions welcomed.
-- Update:
Sorry, let me expand.. I'm not only looking for http:// it needs to detect and wrap links that start with "https://" and/or just start with "www"
Your regex doesn't work because ECMA (JavaScript) doesn't support POSIX character classes, so [:punct:] ignores the . in .com.

Categories

Resources