2016-05-20 19 views
0

サイトに表示されているエンコードされたURLをデコードするためにGoogle Chromeの小さな拡張機能を作成しようとしています。私はいくつかの時間前にJavaでコアメソッドを書き、誰もが持っていた場合はJavaScriptをChrome拡張機能を使用してURLをデコードする

function decodeURL(Encoded) { 
var Length = Encoded.length; 
var Counter = 0; 
var Character; 
var Decoded = ""; 
for (Counter = 0; Counter < Length; Counter++) { 
    Character = Encoded.charAt(Counter); 
    if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 70) { 
     Counter += 2; 
     Decoded += "/"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 65) { 
     Counter += 2; 
     Decoded += ":"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 49) { 
     Counter += 2; 
     Decoded += "!"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 50) { 
     Counter += 2; 
     Decoded += "\""; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 51) { 
     Counter += 2; 
     Decoded += "#"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 52) { 
     Counter += 2; 
     Decoded += "$"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 53) { 
     Counter += 2; 
     Decoded += "%"; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 68) { 
     Counter += 2; 
     Decoded += "="; 
    } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 70) { 
     Counter += 2; 
     Decoded += "?"; 
    } else { 
     Decoded += Character; 
    } 
return Decoded; 

} }の代わりに復号化されたURLの

それは何も(エラーなしなしString)を返しません

に変換しようとしましたエラーが発生する可能性があるアイデアは、私は感謝するだろう。

+0

私は 'Encoded.length == 0 'という賭け - それはありますか? – messerbill

+0

encodeURIComponent/decodeURIComponentはこれに対応していませんか? – Shilly

+1

JavaScriptにはネイティブのデコード/エンコード機能があります。あなたはそれらを使うことを検討していますか? [JavaScript decodeURIComponent()関数](http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp) –

答えて

0
forループの外で、あなたのリターンを移動

function decodeURL(Encoded) { 
    var Length = Encoded.length; 
    var Character; 
    var Decoded = ""; 
    for (var Counter = 0; Counter < Length; Counter++) { 
     Character = Encoded.charAt(Counter); 
     if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 70) { 
      Counter += 2; 
      Decoded += "/"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 65) { 
      Counter += 2; 
      Decoded += ":"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 49) { 
      Counter += 2; 
      Decoded += "!"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 50) { 
      Counter += 2; 
      Decoded += "\""; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 51) { 
      Counter += 2; 
      Decoded += "#"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 52) { 
      Counter += 2; 
      Decoded += "$"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 50 && Encoded.charAt(Counter + 2) === 53) { 
      Counter += 2; 
      Decoded += "%"; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 68) { 
      Counter += 2; 
      Decoded += "="; 
     } else if (Character === 37 && Encoded.charAt(Counter + 1) === 51 && Encoded.charAt(Counter + 2) === 70) { 
      Counter += 2; 
      Decoded += "?"; 
     } else { 
      Decoded += Character; 
     } 

    } 
    return Decoded; 
} 
console.log(decodeURL("???$$$?")); 
関連する問題