如果文本中有反斜杠作为文本的一部分,如何使用 MozRepl 在文本框中输入文本?

How to enter text into textbox using MozRepl if the text has backslashes in it as part of the text?

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/([\"])/\/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 行的文本框时,不允许使用反斜杠或任何特殊字符。

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

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

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

问题已解决。稍后用一个字符替换反斜杠。

看看这个答案。我必须创建一个本地 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/([\"])/\/g, s/\n/\r\n/g for $para_text;

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

产出

此输出正确表示 "awkward" 个字符换行符和双引号,这会使 JavaScript 字符串的语法无效。它使用了我在对您之前的问题

的评论中提出的完全相同的替换
s/([\"])/\/g, s/\n/\n/g for @para_text;