简体   繁体   中英

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/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

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

I have the above code.

The array holds a email template and the script is made around the Outlook webapp using WWW::Mechanize::Firefox .

The email template has a directory in it which has backslashes.

MozRepl does not allow backslashes or any special characters when putting the text into the text box at the $mech->eval_in_page line.

How can I put backslashes in the text if it's not allowed by the module?

Take a look at this answer. I've had to create a local HTML file, and have assumed that you're using <textarea /> , as a simple <input type="text" /> won't accept multiple lines

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>

Here I've used the data from your question, with the exception of duplicating $para_text[4] to fix the interpolation of $build and to test double quotes (around "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"/ );

output

This output correctly represents the "awkward" characters newline and double quote, which would invalidate the syntax of the JavaScript string. It uses exactly the same substitution that I proposed in my comments to your earlier question Trying to interpolate an array

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

txtbdy.pl的输出

I removed the backslashes from my code and used this line to substitute them in

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

Problem Solved. Substituting a character for the backslashes later on works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM