改进我的JavaScript文本加扰器

时间:2017-02-22 08:49:17

标签: javascript

这是我到目前为止所得到的:https://jsfiddle.net/CRASNY/dms3tvjj/6/

function scramble(a){a=a.split("");for(var b=a.length-1;0<b;b--){var c=Math.floor(Math.random()*(b+1));d=a[b];a[b]=a[c];a[c]=d}return a.join("")}

function scrambleText(){
console.log(1);
var textArea = document.getElementById('TEXTAREA_ID');
var lines = textArea.value.split('\n');
for(var i = 0;i < lines.length;i++){
    lines[i] = scramble(lines[i]).toUpperCase().split('').join(' ');
}
textArea.value = lines.join('\n');
}

document.getElementById('BUTTON_ID').onclick = scrambleText;

你可以看到它接受输入并对其进行加扰,将大小写更改为大写,并在每个字母之间放置一个空格。但是,对于多个单词长的字符串,这不会那么好。

例如,This sentence is a test目前被加密为S T E S A I S S E C I E T E T H N T N

我想要实现的目标:字符串的每个单词都被单独加扰并由~键包装。我也希望输出显示在输出中,在加扰文本之后用|分隔。 This sentence is a test的示例输出为~H T S I~ ~E C N T E S N E~ ~S I~ ~A~ ~T T S E~|This sentence is a test。任何人都可以指导我如何做到这一点吗?

2 个答案:

答案 0 :(得分:1)

你可以添加另一个分隔单词和扰乱的函数,然后使用tidle字符最后加入它们:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE");

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

System.setProperty("webdriver.ie.driver","C://MavenTest//driver//IEDriverServer.exe");

driver = new InternetExplorerDriver();

function scramblePhrase(_phrase) { var words = _phrase.split(/\s+/); words = words.map(function(_word) { return scramble(_word); }); return words.join('~').toUpperCase(); } function scramble(a){a=a.split("");for(var b=a.length-1;0<b;b--){var c=Math.floor(Math.random()*(b+1));d=a[b];a[b]=a[c];a[c]=d}return a.join("")} 功能中,拨打scrambleText()而不是scramblePhrase()

以下是您修改后的版本:https://jsfiddle.net/dms3tvjj/8/

答案 1 :(得分:0)

要保留原始实现的形式,您必须添加一个额外的迭代层来处理您的案例的单词分离。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <select name="type" class="minimal">
    <option value="Record" hidden>Record</option>
    <option value="NS" selected data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
    <option value="MX" data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX </option>
  </select>

  <br/>

  <input type="text" name="hostInput" placeholder="Hostname" class="to_validate" title="Wrong host" />
  <br/>

  <br/>

  <select name="type" class="minimal">
    <option value="Record" hidden>Record</option>
    <option value="NS"  data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
    <option value="MX" selected data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX </option>
  </select>

  <br/>

  <input type="text" name="hostInput" placeholder="Hostname" class="to_validate" title="Wrong host" />
  <br/>

  <br/>

  <select name="type" class="minimal">
    <option value="Record" hidden>Record</option>
    <option value="NS"  data-pattern="(?!\d+(?:\.\d+){3}$)(([a-zA-Z\d]|[a-zA-Z\d][a-zA-Z\d-]*[a-zA-Z\d])\.)*([A-Za-z\d]|[A-Za-z\d][A-Za-z\d-]*[A-Za-z\d])" data-placeholder="Hostname" data-title="Wrong host">NS</option>
    <option value="MX" selected data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">MX </option>
    
    <option value="TX" selected data-pattern="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" data-placeholder="IP address" data-title="Wrong Ip Placeholder">TX </option>
  </select>

  <br/>

  <input type="text" name="hostInput" placeholder="Hostname" class="to_validate" title="Wrong host" />
  <br/>

</form>

考虑到在迭代中再添加一个级别不会导致混乱,在bin中添加更多级别或更宏大的数据操作将很快导致代码难以理解。查看techfoobar的答案,找出重新组织代码的可能方法,以提高可读性。 请参阅此处的工作示例:https://jsfiddle.net/dms3tvjj/9/