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

我会使用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_шеллы"

因此,无需创建自己的函数。

将您的返回移出循环:

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

最新更新