How to replace the forward slash in a javascript bookmarklet - javascript

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(/\/$/, ""));

Related

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

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.

display current subdirectory URL of webpage

Having an issue with the following code, it displays the full URL of the current page, (eg, example.com/dir1) however I am looking to display the sub directories only (eg, /dir1/). Also having an issue where it does not display spaces properly, spaces show in html encoding %20. I have very little programming experience and any help would be greatly appreciated.
<p3><script>document.write(location.href);</script></p3>
EDITworking on the following script, however am having trouble implementing it
<script>str.replace("%20", " ")</script>any thoughts?
EDIT - answer which suited my needs, many thanks to brettc
var url = location.href;
url = url.split("examle.com").pop();
url = decodeURIComponent(url);
document.write(url);
This may not be the best way, but a way none the less.
You could just split the string by the ”/“ and take the last occurrence.
Also, decodeURIComponent() will decode the %20 to a space, as found here.
<script>
var url = location.href;// get url, put in url variable
url = url.split("/").pop();// get last element separated by “/“
url = decodeURIComponent(url);// remove any %20
alert(url);
</script>
Note: this will alert everything past the last “/“.

How to remove URL from a string completely in Javascript?

I have a string that may contain several url links (http or https). I need a script that would remove all those URLs from the string completely and return that same string without them.
I tried so far:
var url = "and I said http://fdsadfs.com/dasfsdadf/afsdasf.html";
var protomatch = /(https?|ftp):\/\//; // NB: not '.*'
var b = url.replace(protomatch, '');
console.log(b);
but this only removes the http part and keeps the link.
How to write the right regex that it would remove everything that follows http and also detect several links in the string?
Thank you so much!
You can use this regex:
var b = url.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
//=> and I said
This regex matches and removes any URL that starts with http:// or https:// or ftp:// and matches up to next space character OR end of input. [\n\S]+ will match across multi lines as well.
Did you search for a url parser regex? This question has a few comprehensive answers Getting parts of a URL (Regex)
That said, if you want something much simpler (and maybe not as perfect), you should remember to capture the entire url string and not just the protocol.
Something like
/(https?|ftp):\/\/[\.[a-zA-Z0-9\/\-]+/
should work better. Notice that the added half parses the rest of the URL after the protocol.

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.

How can I submit a hash key in an URL parameter?

I have a Spring-MVC application with Freemarker as the view component.
In my templates, several links are generated which point back to my application and which include URL parameters containing a hash key (#).
Example:
parameter: Q#106368 11
URL generated by Freemarker with encoded param: testurl.html?key=Q%23106368%2011
I use JavaScript to redirect to this URL (reason: I use JS to manage loading of 2 frames at the same time).
The redirect method is simple:
function redir(url) {
window.location.href = url;
}
The JS call generated by Freemarker looks like
test
My problem is that the browser / Javascript converts back the URL encoded parameter, thinks there is a # and cuts off there.
When I use window.location.href='http://...' directly it works. Only when using the method parameter it seems to be magically URL decoded and then the redirect fails because the URL gets cut off at the #.
Is there an easy way to transmit the parameter correctly?
I am aware that I could replace the #, e.g. with $$$hash$$$, in the template and do the replacement on the server side again. But there are so many places I would have to change...
As Marc B commented, it is necessary to URL encode again. The method would be encodeURI(). However, this method does not encode the # sign. For my specific use case, I have to replace the # sign with %23 after the encoding.
The redirect JS method finally looks like:
function redir(url) {
url = encodeURI(url);
url = url.replace(/#/g, '%23');
window.location.href = url;
}
Comparing escape(), encodeURI(), and encodeURIComponent()
encodeURIComponent/decodeURIComponent is more thorough than just encodeURI, it will decode/encode '#' and event '/'
What browser are you using? I'm trying FireFox 5 and it doesn't convert %23 back into # in my testing. When you mouse over the link or copy the link location, what does that have? Are you sure whatever is outputting the link isn't doing the conversion?
Update
This isn't ideal, but it seems like it solves the problem:
<a onclick="url = 'http://localhost:8080/testappp/testurl.html?key=Q%23106368%2011';" href="javascript:redir(url);">test</a>
It seems like the href attribute is decoded. When I mouse over it I seen the # instead of the %23.

Categories

Resources