如果文本在文本中包含反斜杠,如何使用MozRepl将文本输入到文本框中?

时间:2017-04-10 10:07:54

标签: perl www-mechanize www-mechanize-firefox mozrepl

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"/ );

我有上面的代码。

该阵列包含一个电子邮件模板,该脚本是使用WWW::Mechanize::Firefox围绕Outlook webapp制作的。

电子邮件模板中有一个目录,里面有反斜杠。

将文本放入MozRepl行的文本框时,

$mech->eval_in_page不允许使用反斜杠或任何特殊字符。

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

2 个答案:

答案 0 :(得分: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"附近)< / p>

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"/ );

输出

此输出正确表示&#34;尴尬&#34;字符换行符和双引号,这将使JavaScript字符串的语法无效。它使用了我在评论中提出的与您之前的问题Trying to interpolate an array

完全相同的替换
s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

output from txtbdy.pl

答案 1 :(得分:-3)

我从代码中删除了反斜杠,并使用此行代替

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

解决了问题。在后来用字符代替反斜杠的作品。