简体   繁体   English

iMacros:使用Javascript和EVAL替换变量中的文本

[英]iMacros: Using Javascript with EVAL to replace text in a variable

I'm using iMacros on Firefox to download my Wells Fargo PDF bank statements automatically, and give the files the name of the link, like " Statement 04/22/12 (597K) " 我在Firefox上使用iMacros自动下载我的Wells Fargo PDF银行对帐单,并为文件提供链接名称,例如“ Statement 04/22/12(597K)

However, I can't have slashes in the file name (Windows restriction...). 但是,我不能在文件名中添加斜杠(Windows限制...)。 So I'm trying to replace the forward slashes by a dash . 所以我试图用破折号替换正斜杠

Here is my iMacro below. 这是我的iMacro。 I get an error code on the following line: 我在以下行中收到错误代码:

SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; s.replace(/\//g, "-");") 

My javascript or my Regex doesn't work, and I don't know why. 我的javascript或我的正则表达式不起作用,我不知道为什么。 I'm not strong in either language - I've tried to copy from other examples as best as I could, but with no success. 我用这两种语言都不强 - 我试图尽可能地从其他例子中复制,但没有成功。 If you can help, that'd be great. 如果你能提供帮助,那就太好了。 Thanks. 谢谢。

VERSION BUILD=5010424 RECORDER=CR
' SET !ERRORIGNORE YES

TAB OPEN
TAB T=2
URL GOTO=https://online.wellsfargo.com/login
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:Signon ATTR=ID:username CONTENT=XXX
SET !ENCRYPTION TMPKEY
TAG POS=1 TYPE=INPUT:PASSWORD FORM=ID:Signon ATTR=ID:password CONTENT=XXX
TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:Signon ATTR=NAME:continue&&VALUE:Sign<SP>On
TAG POS=1 TYPE=A ATTR=TXT:View<SP>online<SP>statements

SET !EXTRACT_TEST_POPUP NO

TAG POS=1 TYPE=A ATTR=TXT:Statement<SP>* EXTRACT=TXT
SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\"; s.replace(/\//g, "-");")    <---- THIS IS THE PROBLEMATIC LINE!!!!!!!!!!!!!!
ONDOWNLOAD FOLDER=D:\Wells<SP>Fargo\CREDIT<SP>CARD\ FILE={{!VAR1}}.pdf
TAG POS=1 TYPE=A ATTR=TXT:Statement<SP>* CONTENT=EVENT:SAVETARGETAS

TAB CLOSE

As an extra, if you know how to remove the " (597K) " with Regex in the javascript, that'd be the icing on the cake. 另外,如果你知道如何在javascript中删除带有正则表达式的“ (597K) ”,这将是锦上添花。 (obviously, the value of this text string will be different for each different file, so it has to be dynamic) (显然,对于每个不同的文件,此文本字符串的值将不同,因此它必须是动态的)

You missed escaping the quotes in "-" , so ended the iMacros string instead of starting a JavaScript string. 您错过了"-"的引号转义,因此结束了iMacros字符串而不是启动JavaScript字符串。

You've also potentially got a problem in the /\\// regexp, because I suspect iMacros will eat the \\/ and turn it into / , leaving JS with the invalid literal /// . 你也可能在/\\// regexp中遇到了问题,因为我怀疑iMacros会吃\\/并把它变成/ ,让JS留下无效的文字/// It would have to be /\\\\// . 它必须是/\\\\//

Also, if the !EXTRACT variable contains quotes, backslashes, or newlines, it will inject those into the JavaScript code directly, breaking the JS string literal and potentially executing arbitrary script. 此外,如果!EXTRACT变量包含引号,反斜杠或换行符,它将直接将这些内容注入JavaScript代码,从而破坏JS字符串文字并可能执行任意脚本。

These are examples of the many problems with nested escaping contexts. 这些是嵌套转义上下文的许多问题的示例。 You can avoid the first two by using alternate non-clashing quotes, and getting rid of the regex literal by using the simple string replace split / join idiom instead: 您可以通过使用备用非冲突引号来避免前两个,并通过使用简单字符串替换split / join idiom来删除正则表达式文字:

SET !VAR1 EVAL("'{{!EXTRACT}}'.split('/').join('-').split(' (')[0]") 

but this still leaves the JS injection problem. 但这仍然留下了JS注入问题。 From a quick look at the manual, it seems iMacros doesn't have any usable string processing functionality, so you wouldn't be able to do a manual JS-string-literal-replace, or indeed just do the / -to - replacement from within iMacros. 从一个简单看一下说明书,似乎iMacros都没有任何可用的字符串处理功能,所以你不能够做一个手动JS-字符串文本替换,或者实际上只是做/ -to -更换来自iMacros。 That would have been the sensible thing to do; 那将是明智的做法; creating executable code from strings is almost always a disastrously wrong thing to strenuously avoid. 从字符串创建可执行代码几乎总是强烈避免的灾难性错误。

[aside rant: From that look at the manual, the existence of iMacros makes me genuinely unhappy. [除了咆哮:从手册上看,iMacros的存在让我真的很不开心。 It's offering you a completely arbitrary yet incapable homebrew scripting language instead of just using the already-available JavaScript, then making you go to JS anyway to do anything beyond the laughably trivial... and not giving you tools to transfer values between the two safely. 它为您提供了一个完全随意但无法使用的自制脚本语言,而不仅仅是使用已经可用的JavaScript,然后让你去JS,除了可笑的琐碎之外做任何事情......并没有给你工具来安全地传递两者之间的价值。 What is even the point of this software? 这个软件的重点是什么? I haven't felt this frustrated with trying to work around wrong-headed language design since the dark days of 4GLs. 自从4GL的黑暗日子以来,我一直没有感到因为试图解决错误的语言设计而感到沮丧。 This is just awful. 这太可怕了。 People use this?] 人们用这个?]

Your task, like most non-trivial scraping problems can be solved much easier using javascript to organize your imacros code 使用javascript来组织你的imacros代码可以更轻松地解决你的任务,就像大多数非平凡的抓取问题一样

Take a look at this example. 看看这个例子。 I don't use Wells Fargo so I can't test the actual downloading but the basic skeleton is there 我不使用富国银行,所以我无法测试实际的下载,但基本的骨架就在那里

run()
function run() {
  // loginResult is null on success
  var loginError = performLogin()
  if (loginError) {
    alert(JSON.stringify(loginError, null, ' '))
    throw new Error(JSON.stringify(loginError))
  }
  // loginResult is null on success
  var extractError = extractStatement()
  if (extractError) {
    alert(JSON.stringify(extractError, null, ' '))
    throw new Error(JSON.stringify(extractError))
  }
  iimDisplay('Download completed successfully')
}

/**
 * @return null on success, error object on failure
 */
function extractStatement() {
  var error,
      code,
      extract
  code = iimPlay('CODE: TAG POS=1 TYPE=A ATTR=TXT:View<SP>online<SP>statements\n'
                 + 'TAG POS=1 TYPE=A ATTR=TXT:Statement<SP>* EXTRACT=TXT')
  if (code !== 1) {
    error = {
      message: 'error extracting statement',
      error: iimGetLastError(),
      errorCode: code
    }
  }
  extract = iimGetLastExtract()

  // #EANF# means Extract Anchor Not Found (ie extract failed)
  if (extract === '#EANF#') {
    error = {
      message: 'error extracting statement',
      error: 'extract not found'
    }
  }

  // the magic line to replace all slashes
  var filename = extract.replace(/\//g, '-')
  // add the .pdf extension
  filename = filename + '.pdf'

  // download with the new filename, the double slashes are needed because
  // javascrtip otherwise views a backslash as an escape character
  var folderPath = 'D:\\Wells<SP>Fargo\\CREDIT<SP>CARD\\'
  var downloadCode = iimPlay('CODE: ONDOWNLOAD FOLDER=' +folderPath + '  FILE=' + filename + '\n'
                             + 'TAG POS=1 TYPE=A ATTR=TXT:Statement<SP>* CONTENT=EVENT:SAVETARGETAS\n'
                            + 'TAB CLOSE')

  if (downloadCode !== 1) {
    error = {
      message: 'failed to download statement',
      error: iimGetLastError(),
      errorCode: downloadCode
    }
    return error
  }

  // download completed correctly
  return null
}

/**
 * @return null on success, error object on failure
 */
function performLogin() {
  var code = iimPlay('CODE: TAB OPEN\n'
                     + 'TAB T=2\n'
                     + 'URL GOTO=https://online.wellsfargo.com/login\n'
                     + 'TAG POS=1 TYPE=INPUT:TEXT FORM=ID:Signon ATTR=ID:username CONTENT=XXX\n'
                     + 'SET !ENCRYPTION TMPKEY\n'
                     + 'TAG POS=1 TYPE=INPUT:PASSWORD FORM=ID:Signon ATTR=ID:password CONTENT=XXX\n'
                     + 'TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:Signon ATTR=NAME:continue&&VALUE:Sign<SP>On\n')

  // code will be 1 on success
  if (code === 1) {
    return null
  }
  var error = {
    message: 'error performing login',
    error: iimGetLastError(),
    errorCode: code
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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