Get auth token from URL - javascript

I am trying to get a Twitter oauth token using the below string. How can I "run" the string and then get the token?
This is the request string:
https://api.twitter.com/oauth/request_token?oauth_consumer_key=9TL0JGKTIv7GyOBeg8ynuxg&oauth_nonce=Xty48&oauth_signature=3skps99e6zkn0rcUGadVUEuHFon4%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1346603336
This is the result:
oauth_token=TBFdNoytaizrfMAWNZ6feqNz3BsozHk5AesIioX8u8Ec&oauth_token_secret=DtQ3jiUIeVdRcBAKwVQJRWpgKtEHi3m1ylk0nlsHCBj0&oauth_callback_confirmed=true

See answer to this question. It demonstrates how to get a value from the querystring.
Here, I've altered it to take the name value collection as a parameter:
function getParameterByName(name, qs)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(qs);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Usage:
var result = "oauth_token=TBF...&oauth_token_secret=DtQ...";
var token = getParameterByName("oauth_token", result);

Related

How to write regexp to get a paramater from URL

I need help with a regexp to get only the code parameter from this url:
URL: code=4%2FsAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I&scope=https:.....
My Code:
getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$|)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
this piece of code is returning `
4/sAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I
In this case i need the first and only backslash to be a % sign instead..
I can't get it to work.
Thnx in advance!
It works for me!!!
function getUrlParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Untested JavaScript regex:
var myRe = /=(.*)&/igm
var url = 'code=4%2FsAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I&scope=https:';
var code = myRe.exec(url);
Use \bcode=([^&]+). It's sure to grab code query using word boundary 'code=' then capture any characters until it reaches & or the end.
test regex here: https://regex101.com/r/eSnB4S/1

How to update internal links parameters based on current URL using jQuery?

How we can update the internal links of the page if some visits from specific URL.
For Example.
Some Users comes from Facebook and URL is
https://www.example.com/?utm_source=facebook&utm_medium=cpc&utm_campaign=mobile
and I want to update all my particular links to update based on URL, In case of facebook
Original URL
https://www.example.com/cat/i?pid=ABCD&affid=aff1&affExtParam1=para1&affExtParam2=para2
Updated URL after User visit from facebook link
https://www.example.com/cat/i?pid=ABCD&affid=aff1&affExtParam1=facebook&affExtParam2=mobile
Even If It can edit one parameter in URL that will be helpful.
Note:
In param1 and param2 can be any text in exiting page.
Thanks for the hints. I have tried and able to replace one URL Parameters. Below is the code.
Combined scripts from different answers and use below code.
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)", "i");
if( value === undefined ) {
if (uri.match(re)) {
return uri.replace(re, '$1$2');
} else {
return uri;
}
} else {
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
} else {
var hash = '';
if( uri.indexOf('#') !== -1 ){
hash = uri.replace(/.*#/, '#');
uri = uri.replace(/#.*/, '');
}
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
return uri + separator + key + "=" + value + hash;
}
}
}
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return decodeURI(results[1]) || 0;
}
}
var key = "affExtParam2";
var value = $.urlParam('utm_source');
$('a').each(function(){
var uri = $(this).attr("href");
$(this).attr("href" , updateQueryStringParameter( uri, key, value ));
});
Not able to test in JS fiddle but working correctly in local, if you guys can further optimize that will be great help.

how to add substring into a string

I have a url and i need to enter a port number to the url.
the url is not a valid url.
here is few show cases :
https://example.com_users/param/param/param - https://example.com_users:8080/param/param/param
http://example.co_setting/param/param/param - http://example.co_settings:1000/param/param/param
http://example.co_setting- http://example.co_settings:1000
const addPort = (url,port) =>{
combined = ???????? // how to combian them
return combined
}
You could use a regular expression:
const addPort = (url, port) =>
url.replace(/^(https?:\/\/)?([^/]*)(\/.*)?$/, '$1' + '$2:' + port + '$3');
console.log(addPort('http://www.example.com/full/url/with/param', '8080'))
var urlstring = 'https://example.com_users/param/param/param';
var port = ':8080';
var allparts = urlstring.split('//');
var last = allparts[1];
var alllastparts = last.split('/');;
alllastparts[0] = alllastparts[0]+port;
alert(allparts[0]+ '//' + alllastparts.join('/'));
console.log(allparts[0]+ '//' + alllastparts.join('/'));

Trying to extract information from querystring

I'm new to JavaScript and I am trying to extract information from a query string on the web I created, if I load straight on the page without the query string the page will load but once I redirect from my form page to the page where I'm doing the parsing it freezes and crashes... can anyone help please! :(
http://main.xfiddle.com/7d679c3a/Project1/Commission.php
http://main.xfiddle.com/7d679c3a/Project1/contactForm.php
http://main.xfiddle.com/7d679c3a/Project1/DiceRoll.php
http://main.xfiddle.com/7d679c3a/Project1/IsEven.php
http://main.xfiddle.com/7d679c3a/Project1/palindrome.php
http://main.xfiddle.com/7d679c3a/Project1/part1.php
http://main.xfiddle.com/7d679c3a/Project1/passwordStrength.php
http://main.xfiddle.com/7d679c3a/Project1/allinOne.php
JavaScript Code
var $ = function(id)
{
return document.getElementById(id);
}
var formInfo = location.search();
formInfo = formInfo.substring(1, formInfo.length);
while (formInfo.indexOf("+") != -1)
{
formInfo = formInfo.replace("+", " ");
}
while (formInfo.indexOf("=") != -1)
{
formInfo.replace("=", " ");
}
formInfo = decodeURI(formInfo);
formInfo.replace("firstname", "");
formInfo.replace("lastname", "");
formInfo.replace("phonenumber", "");
formInfo.replace("postalcode", "");
formInfo.replace("startingmoney", "");
var infoArray = formInfo.split("&");
var firstName = infoArray[0];
var lastName = infoArray[1];
var phoneNumber = infoArray[2];
var postalCode = infoArray[3];
var startingMoney = infoArray[4];
$("playername").innerHTML = firstName + " " +lastName;
$("playerinfo").innerHTML = phoneNumber + " " + postalCode;
$("money").innerHTML = " $$" + startingMoney;
HTML Code
<div id="fireinfo">
<p id="playername"></p><br/>
<p id="playerinfo"></p>
<p id="money"></p>
</div>
I want to out put the information i get from the query string into the player name, player info and money id's.
Use this function to get query string value:
function getQueryString(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(window.location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
var firstname = getQueryString('firstname');

Javascript function to strip user ID from URL

i need a javascript function to strip the user ID from a given URL.
for example. i have this URL http://www.example.com/member.php?id=100001877097904
how can i retrieve the ID of the user from that URL?
Thanks in advance.
function getParameterByName(url, name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
One possible solution:
var url = "http://www.example.com/member.php?id=100001877097904",
id = (url.match(/[?&#]id=(\d+)/) || []).pop();
console.log(id);

Categories

Resources