0
my @para_text = (
"The build of $build CUT$cut was started as requested and 
its progress can be monitored in Anthill here", 
"",
"http://anthill:8080/tasks/project/BuildLifeTasks/viewBuildLife?
buildLifeId=$lifeid", 
"", 
'If it completes successfully (Overall Anthill status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\mizar\release\AnthillRelease\$build', 
"", "If the output exists but the anthill build was not completely 
successful DO NOT attempt to copy and deploy the output. \n", 
"We will send the usual email detailing content etc. when the build 
finishes. \n");

$para_text[0] =~ s/[\r\n]//gm;    # convert multiline to single line

@para_text = join "\n", @para_text;

s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

$mech->eval_in_page( qq/document.getElementsByName("txtbdy")[0].value = "@para_text", "test"/ );

我有上面的代码。

该数组包含一个电子邮件模板,脚本是围绕 Outlook webapp 使用WWW::Mechanize::Firefox.

电子邮件模板中有一个带有反斜杠的目录。

MozRepl将文本放入该行的文本框中时,不允许使用反斜杠或任何特殊字符$mech->eval_in_page

如果模块不允许,如何在文本中添加反斜杠?

4

2 回答 2

0

看看这个答案。我不得不创建一个本地 HTML 文件,并假设您正在使用<textarea />,因为简单<input type="text" />不会接受多行

txtbdy.html

<html>
  <head>
    <title>Textbox test</title>
    <style type="text/css">
      #textbdy {
        width:  800;
        height: 300;
        resize: none;
      }
    </style>
  </head>
  <body>
    <form>
        <textarea name="txtbdy" id="textbdy" />
    </form>
  <body>
</html>

在这里,我使用了您问题中的数据,除了复制$para_text[4]以修复双引号的插值$build和测试双引号(围绕"Athill"

txtbdy.pl

    use utf8;
    use strict;
    use warnings 'all';

    use WWW::Mechanize::Firefox;

    my $mech = WWW::Mechanize::Firefox->new(
        tab      => qr/Textbox test/,
        create   => 1,
        activate => 1,
    );
    $mech->autoclose_tab( 0 );

    $mech->get( 'file://E:/Perl/source/txtbdy.html' );

    my $build  = "BUILD";
    my $cut    = "CUT";
    my $lifeid = "LIFEID";

    my @para_text = (
        "The build of $build CUT$cut was started as requested and 
its progress can be monitored in Anthill here",
        "",
        "http://anthill:8080/tasks/project/BuildLifeTasks/viewBuildLife?
buildLifeId=$lifeid",
        "",
        'If it completes successfully (Overall Anthill status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\mizar\release\AnthillRelease\$build',
        "",
        qq{If it completes successfully (Overall "Anthill" status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\\mizar\\release\\AnthillRelease\\$build},
        "",
        "If the output exists but the anthill build was not completely 
successful DO NOT attempt to copy and deploy the output. \n",
        "We will send the usual email detailing content etc. when the build 
finishes. \n"
    );

    my $para_text = join "\n", @para_text;

    s/([\\"])/\\$1/g, s/\n/\\r\\n/g for $para_text;

    $mech->eval_in_page( qq/document.getElementsByName("txtbdy")[0].value = "$para_text"/ );

输出

此输出正确地表示“尴尬”字符换行符和双引号,这将使 JavaScript 字符串的语法无效。它使用与我在对您之前的问题的评论中提出的完全相同的替换尝试插入数组

s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

txtbdy.pl 的输出

于 2017-04-12T15:35:27.230 回答
-3

我从我的代码中删除了反斜杠,并使用这一行将它们替换为

$para_text[4] =~ s{!}{\\}g;

问题解决了。稍后在作品中用一个字符代替反斜杠。

于 2017-04-12T08:25:23.370 回答