通过Chrome扩展程序解码网址

时间:2016-05-20 14:06:45

标签: javascript

我正在尝试为Google Chrome编写一个小扩展程序来解码网站上显示的编码网址。我前段时间用Java编写了核心方法,并尝试将其转换为JavaScript

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,它不返回任何内容(没有错误,没有字符串) 如果有人知道错误可能在哪里,我会很感激。

2 个答案:

答案 0 :(得分:1)

我会使用JS的内置函数 encodeURI()decodeURI()

decodeURI("https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B");

返回“https://developer.mozilla.org/ru/docs/JavaScript_шеллы

所以不需要创建自己的功能。

答案 1 :(得分:0)

将你的回报移到外面进行循环:

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("???$$$?"));