How do I know if a URL is decoded / encoded?

I am using the Javascript method decodeURIComponentto decode the encoded URL. Now I have a problem: sometimes the URL is encoded twice during redirection between servers, sometimes it is encoded only once.

I want to check if the url is still encoded after a method call decodeURIComponent. How can i do this? Any pointer would be very helpful to me.

Update - 1

If I recursively call a method and verify that if the given URL still contains "%", if it contains "%", then decode it and call the method again; and if you don't return it to the caller, will it work?

Update - 2

In my case, I have:

callBackUrl=http%253A%252F%252Fadbc.com%252FPOSM%252Fapp%252Fpages%252Fadf.task-flow%253Fadf.tfDoc%253D%25252FWEB-INF%25252Ftask-flows%25252Fcatalog-edit-task-flow.xml%2526adf.tfId%253Dcatalog%2526_adf.ctrl-state%253Db9akorh22_9%2526articleReference%253D10C00135%2526previousView%253Dcatalog-home%2526fromUCM%253Dtrue%2526articleType%253Dposm%2526developer%253Dcentral

Now I take the value of callBackUrl in my js method, then decrypt it and run window.open()with this decoded URL. the parameters are the same and it has:

  • adf.tfDoc
  • adf.tfId
  • articleReference
  • previousView
  • fromUCM
  • ArticleType of article
  • Developer

Options in it. Therefore, I know that there is no query string, for example value="%..".

Update - 3

I wrote the following method:

var decodeURLRecursively = function(url) {
    if(url.indexOf('%') != -1) {
        return decodeURLRecursively(decodeURIComponent(url));
    }

    return url;
}
+11
source share
4 answers

Re-decoding until you find any characters %will work more than 99% of the time. It will work even better if you re-name so that you can find a match for /%[0-9a-f]{2}/i.

, ( - ) 100%achieved, , %ac ¬, . , .

, - , , .

+5

, URL.

. , ; , .

URL-, :

function isEncoded(uri) {
  uri = uri || '';

  return uri !== decodeURIComponent(uri);
}

, isEncoded while ( ), , decodeURIComponent :

function fullyDecodeURI(uri){

  while (isEncoded(uri)){
    uri = decodeURIComponent(uri);
  }

  return uri;
}

URL-, . , .

+14
function checkEncodeURI(str) {
 return /\%/i.test(str)
}

Test:

let url1 = "https://quora.com/unanswered/I’m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I’ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one"
let url2 = 'https://www.quora.com/unanswered/I%E2%80%99m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I%E2%80%99ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one'
let a = checkEncodeURI(url1)
console.log(a)
let b = checkEncodeURI(url2)
console.log(b)
0
source

you can continue to decode until the string changes:

str = "xxxxxxx"
dec_str = decode(str)
while(dec_str != str)
     str = dec_str;
     dec_str = decode(str);
0
source

Source: https://habr.com/ru/post/1656394/


All Articles