I am trying to build a bookmarklet that will grab the URL from a link that is blocked on my network and run a process on the query string parameters.
I have tried to use window.location.href, but all that is returned is URL of the browsers error page that is displayed, not the URL that generated the error (even though that URL is in the address bar up top.
I get nothing with document.referrer.
This is an example of a URL that this should work on:
https://ue.flipboard.com/usage?data=%7B%22prod_type%22%3A%22notification%22%2C%22event_category%22%3A%22email%22%2C%22event_action%22%3A%22click%22%2C%22event_data%22%3A%7B%22type%22%3A%2210today.media.sat.20210710.436.2%22%2C%22target_id%22%3A%22104t%2F0%22%2C%22url%22%3A%22https%3A%2F%2Fflipboard.com%2F%40businessinsider%2Flabor-shortages-and-calls-for-higher-pay-continue-across-the-us-v632ten2kt4p63a4%3Frefresh%3D1%22%2C%22method%22%3A%22web%22%2C%22redirect_url%22%3A%22https%3A%2F%2Fflipboard.com%2F%40businessinsider%2Flabor-shortages-and-calls-for-higher-pay-continue-across-the-us-v632ten2kt4p63a4%3Frefresh%3D1%22%7D%2C%22properties%22%3A%7B%22uid%22%3A%228606374%22%2C%22unique_id%22%3A%228606374%22%2C%22time%22%3A1626124956022%2C%22ab_tests%22%3A%22436_2%22%7D%7D
javascript:(function() {
let cleanURL = new URL(window.location.href);
console.log(cleanURL);
let urlObj = cleanURL.searchParams.get("data");
urlObj = decodeURIComponent(urlObj);
let jsonObj = JSON.parse(urlObj);
window.location.href=jsonObj.event_data.url;
}());
Related
How to check if a window.locaton is successful loaded?
I would like to open a chrome snippet to redirect to a new URL and get the new URL link using console.log.
var long_url = "https://stackoverflow.com/";
var long_url1 = "https://google.com/";
window.location.replace(long_url);
//How to get new URL link "https://stackoverflow.com/"?
window.location.replace(long_url1);
//How to get new URL link "https://google.com/" again?
To get current page URL try
window.location.href
More precise info
var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url = window.location.href; // Returns full URL (https://example.com/path/example.html)
var origin = window.location.origin; // Returns base URL (https://example.com)
You can use window.location.href to get current URL. So you can write:
var long_url = "https://stackoverflow.com/";
var long_url1 = "https://google.com/";
window.location.replace(long_url);
console.log(window.location.href);
window.location.replace(long_url1);
console.log(window.location.href);
To see the console logs even after redirecting to new URL you can go to inspect -> console -> settings -> check Preserve log. By doing so it will help you see all the console logs before or after redirection
Check this image for viewing console settings
I have a page that takes a URL as an argument and immediately navigates to that URL. This URL may go to another webpage, or it can be a file to download. I don't know ahead of time where the URL will go.
If the URL is a download link, I want to display a message to the user. This can be either by making a div visible, or by any other mechanism. Is there any way for me to accomplish this?
<html><head><script>
var urlParams = new URLSearchParams(window.location.search);
var url = urlParams.get("url");
window.top.location.href = url;
// How do I execute the following lines only if url is pointing to a download?
{
var contentEl = document.getElementById('page-content');
contentEl.style.display='block';
}
</script>
</head>
<body><div id='page-content' style='display: none;'>You may close this page</div></body>
</html>
The only thing I have thought of so far is to add a timeout, but it seems pretty lame. Is there a better way?
I think you can try in this way ,before navigate to the url you can check what file extensions of the url is. so now you can open url if the file extensions is some thing like '.html','.jsp' and so on. Display message if the url extensions is '.zip','.docx'...
var urlParams = new URLSearchParams(window.location.search);
var url = urlParams.get("url");
//check extensions here
if(isFileExtensionsLikeHtmlOrJsp(url)){
window.top.location.href = url;
}else{
//display message
}
I open my app with a deeplink
myscheme://?param1=value1¶m2=value2
How can I get the value of the parameters? I found different posts that treat this subject but the once I tried works on http links I think, I alwayse get a warning telling me that BlobURL object is not supported yet.
var url = new URL(data);
alert(url.searchParams.get("param1"));
I have tried your example, and it works, with slight adjustment:
let myscheme = 'http://www.example.com/?param1=value1¶m2=value2'
var url = new URL(myscheme);
alert(url.searchParams.get("param1"));
More details can be found here.
I'm trying to write a LibreJS compatibility web extension for a website. The intended browser is Icecat version 52+.
A form is broken on this website because it has a proprietary onSubmit script.
When I set the onSubmit of the form to my extension's JavaScript and submit the form, it responds with a URL.
When entered manually by copying and pasting this URL into the URL bar it redirects to the correct page.
However, when I get the exact same URL in Javascript and redirect it like this, the webpage behaves differently and gives an error page saying it thinks it's a crawler.
I have tried every other method for redirecting I could find on Stack Overflow like setting document.location.href, window.location.href, window.location.replace(redirect))
My question is how do I fix this? Code:
var http = new XMLHttpRequest();
http.open('POST', url, true);
http.onload = function (){
console.log("URL in response to post:");
var redirect = // verified this is the correct URL
location.href = redirect; // website gives an error page:
//Error! No saved objectValues at all -- crawler? bad link? filter?
return;
};
http.send(form_data);
On some pages that have an encoded part in the url, like %FC instead of ΓΌ, I get a Uncaught URIError: URI malformed error in the console and the captcha therefore is not working.
Our Project is iso-8859-1 encoded and there is nothing I can change about that.
Do you know a workaround that could fix this? I'm using reCAPTCHA 2.
My solution now is, that before the recaptcha/api.js is executed, I grab the window.location.pathname, get rid of the problematic elements and exchange it in the url with history.pushState. If I'd simply changed the window.location.pathname a page redirect would be the consequence.
Here is my example code:
var oldPath = window.location.pathname;
var newPath = decodeURIComponent( unescape( unescape(oldPath)));
var stateObj = {};
history.pushState(stateObj, "", newPath);