Is it possible to Search and Replace Text on Website?

Discussions and Tech Support related to website data extraction, screen scraping and data mining using iMacros.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Is it possible to Search and Replace Text on Website?

Post by viking » Fri Jul 10, 2020 10:52 pm

Some websites have text fields where I can edit the text. Is it possible for iMacros to search and replace text in the text field?

Here is a simple example:
https://www.w3schools.com/js/tryit.asp? ... yjs_ifthen

I would like to replace "Good Day!" with "Good Night!"
Any ideas?

p.s. The website where I would actually like to replace the text is much more complicated. I will create a dummy account and provide login credentials if someone is willing to help. Maybe start with the simpler example above though..?

iMacros= v8.9.7
Browser1= Waterfox-Portable Classic 2020.06(64-bit)
Browser2= Firefox ESR 52.8.1 (64-bit)
OS= WIN7 (64-bit)
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Sat Jul 11, 2020 1:51 am

viking wrote:
Fri Jul 10, 2020 10:55 pm
viking wrote:
Fri Jul 10, 2020 10:42 pm
chivracq wrote:
Fri Jul 10, 2020 2:55 pm
And hum, concerning your "other"/next Thread about "Search and Replace some Content...", you may want to delete that Thread yourself from the 'iMacros for FF' Sub-Forum and recreate it in the 'Data Extraction' Sub-Forum if you want me to eventually answer it :idea: , or you'll have to wait until the Forum Admin will have moved it to that Sub-Forum, which will probably take "a few Days", ... as I only answer Threads opened in the "correct" Sub-Forum, and this one has nothing specific to the 'iMacros for FF' Sub-Forum...
Hmmm, not sure why it would belong in the data extraction sub-forum since I am trying to write text to a web-page (and my script will partially use javascript). But if I need to move it to get your help, which I always very much appreciate :!:, I will do so.... :wink:
Moved :!:

Yep, seen that, Thanks... :wink:

And hum, the 'Search' Functionality belongs to the 'Data Extraction' Sub-Forum as it relates to the 'EXTRACT' Mechanism, and even more if combined with a 'Replace'...
(Even if the Script I post below doesn't really use the Built-in 'EXTRACT' Mechanism, I prefer to use another Method in this Case :P , or at least for this Site which dictates which Method(s) to use...)

>>>
viking wrote:
Fri Jul 10, 2020 10:52 pm
Some websites have text fields where I can edit the text. Is it possible for iMacros to search and replace text in the text field?

Here is a simple example:
https://www.w3schools.com/js/tryit.asp? ... yjs_ifthen

I would like to replace "Good Day!" with "Good Night!"
Any ideas?

p.s. The website where I would actually like to replace the text is much more complicated. I will create a dummy account and provide login credentials if someone is willing to help. Maybe start with the simpler example above though..?

Code: Select all

iMacros= v8.9.7
Browser1= Waterfox-Portable Classic 2020.06(64-bit)
Browser2= Firefox ESR 52.8.1 (64-bit)
OS= WIN7 (64-bit)

Yep, of course it's possible, shouldn't be very difficult, this one for example which is possibly/probably the easiest and quickest Solution, using the 'EVENT' Mode to extract the existing Content, (=> Copy&Paste to the OS Clipboard), delete it, modify it and re-input it with the new Content...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1

SET Search_Str "Good day!"
SET Replace_Str "Good Evening!"

URL GOTO=https://www.w3schools.com/js/tryit.asp?filename=tryjs_ifthen

'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR="#textareawrapper>DIV>DIV>TEXTAREA" CHARS="ac" MODIFIERS=Ctrl
EVENT TYPE=KEYPRESS SELECTOR="#textareawrapper>DIV>DIV>TEXTAREA" KEY=46

SET CB {{!CLIPBOARD}}
SET New_Code EVAL("var cb='{{CB}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=cb.split(ss).join(rs); z;")
SET !CLIPBOARD {{New_Code}}

WAIT SECONDS=0.2
EVENT TYPE=KEYPRESS SELECTOR="#textareawrapper>DIV>DIV>TEXTAREA" CHAR="v" MODIFIERS=Ctrl
WAIT SECONDS=1

'Click 'Run' Button:
'EVENT TYPE=CLICK SELECTOR="HTML>BODY>DIV:nth-of-type(5)>DIV>BUTTON" BUTTON=0
TAG POS=1 TYPE=BUTTON ATTR=TXT:Run<SP>»
(Tested on iMacros for FF v8.8.2, PM v26.3.3, Win10_x64.)

The mini-'WAIT' Statements are not needed, only meant for "you" to see what is happening...
There are 2 Instances of your "Good Day!" (which is actually "Good day!"), you didn't mention which one(s) you want to replace, so I replace them all, ah-ah...!, and the 'split()' I use for the Global 'replace()' is Case Sensitive...

Enjoy...! 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Is it possible to Search and Replace Text on Website?

Post by viking » Sun Jul 12, 2020 1:50 am

Very Nice!
How did you find the key codes; e.g. KEY=46 to delete the text?

As I mentioned in my p.s. above, I need to implement your "proof-of-concept" solution on two other web pages.

They are on a password protected site for back-testing stock trading strategies. Back-testing is a way to see how your strategy would have performed in the past. I want to test the strategies using different parameter combinations.
Here is the first type of page (An "Algo"):

https://www.quantopian.com/algorithms/5 ... 3f1b283c81

Login credentials:
[removed]

I would like to search & replace several parameters, but lets start with the first. Here is the code that I tried:

Code: Select all

SET Search_Str "MAX_GROSS_EXPOSURE = 1.0"
SET Replace_Str "MAX_GROSS_EXPOSURE = 2.0"

'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="ac"  MODIFIERS=Ctrl
PAUSE
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" KEY=46
PAUSE

SET CB {{!CLIPBOARD}}
SET New_Code EVAL("var cb='{{CB}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=cb.split(ss).join(rs); z;")
SET !CLIPBOARD {{New_Code}}
PAUSE

EVENT TYPE=KEYPRESS SELECTOR="#textareawrapper>DIV>DIV>TEXTAREA" CHAR="v" MODIFIERS=Ctrl
My first problem is that the macro doesn't even select/copy the text in the black box using (obtained by recording and modifying per your code):

Code: Select all

EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="ac"  MODIFIERS=Ctrl
How can I find the correct Event command?
Last edited by viking on Sun Jul 12, 2020 2:33 am, edited 1 time in total.
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Sun Jul 12, 2020 2:08 am

Hum..., don't "wait" too long for Feedback/Follow-up, I very-very rarely write Scripts for other Users, and I most of the time regret it "afterwards", because of "No Follow-up"..., just saying... :wink: :(

>>>

Oh funny, I was trying to post this Reply since 15 min, difficult Internet Co., I still post it, I haven't read your Reply yet...
I think the "No Reply - No follow-up..." Rant doesn't apply anymore...
(I have a Friend at my place now and limited (= difficult) Internet, I might need a while to check and react on your Reply)... (and there seems to be some Comet in the Sky that we MUST manage to see, ah-ah...! :P )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Sun Jul 12, 2020 2:23 am

viking wrote:
Sun Jul 12, 2020 1:50 am
Very Nice!
How did you find the key codes; e.g. KEY=46 to delete the text?

As I mentioned in my p.s. above, I need to implement your "proof-of-concept" solution on two other web pages.

They are on a password protected site for back-testing stock trading strategies. Back-testing is a way to see how your strategy would have performed in the past. I want to test the strategies using different parameter combinations.
Here is the first type of page (An "Algo"):

https://www.quantopian.com/algorithms/5 ... 3f1b283c81

Login credentials:
[... Saved by @chivracq...]

I would like to search & replace several parameters, but lets start with the first. Here is the code that I tried:

Code: Select all

SET Search_Str "MAX_GROSS_EXPOSURE = 1.0"
SET Replace_Str "MAX_GROSS_EXPOSURE = 2.0"

'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="ac"  MODIFIERS=Ctrl
PAUSE
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" KEY=46
PAUSE

SET CB {{!CLIPBOARD}}
SET New_Code EVAL("var cb='{{CB}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=cb.split(ss).join(rs); z;")
SET !CLIPBOARD {{New_Code}}
PAUSE

EVENT TYPE=KEYPRESS SELECTOR="#textareawrapper>DIV>DIV>TEXTAREA" CHAR="v" MODIFIERS=Ctrl
My first problem is that the macro doesn't even select/copy the text in the black box using (obtained by recording and modifying per your code):

Code: Select all

EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="ac"  MODIFIERS=Ctrl
How can I find the correct Event command?

OK, seen it, I've saved the L&P in some Local File, in case you want to remove that Info... [removed from my Quote] :idea:
But like I said in my previous Post a very rarely write Scripts for other Users..., you may want to try "a little bit" by yourself if you manage "to crack it" using the Method I posted, or some other one, it all depends on the Site and its HTML Structure... :|
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Sun Jul 12, 2020 7:29 pm

"How did you find the key codes; e.g. KEY=46 to delete the text?"
=> Yeah well, I simply recorded it, ah-ah...! :P And you have some Tables on Internet as well otherwise...

The 'Backspace' Key would also work, but this Key is sometimes "dangerous" depending on where the Focus is and sometimes gets interpreted by the Browser as "Back", so I preferred the 'Del' Key...

>>>

OK, I had a Look at your Site, hum, quite Interesting actually..., and my original Code works fine, but you've removed an "S" to "CHAR[S]", ah-ah...! :shock:
"EVENT[S]" and "CHAR[S]" both need to be synced for Singular/Plural, "v" is 1 Char, but "ac" is 2 Chars, ah-ah...! :!:

And you had also kept the original Selector for the 'Paste' from the other first Site... Now I've put the Selector in a Var, so you cannot make a Mistake, ah-ah...! 8)

=> This one works for me then...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
'URL GOTO=https://www.quantopian.com/algorithms/5f0a5f7cd986d33f1b283c81

SET Selector "#ide-container>DIV>DIV>TEXTAREA"

SET Search_Str "MAX_GROSS_EXPOSURE = 1.0"
SET Replace_Str "MAX_GROSS_EXPOSURE = 2.0"

'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR={{Selector}} CHARS="ac"  MODIFIERS=Ctrl
EVENT TYPE=KEYPRESS SELECTOR={{Selector}} KEY=46

SET CB {{!CLIPBOARD}}
SET New_Code EVAL("var cb='{{CB}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=cb.split(ss).join(rs); z;")
SET !CLIPBOARD {{New_Code}}

'Paste New Code/Content:
EVENT TYPE=KEYPRESS SELECTOR={{Selector}} CHAR="v" MODIFIERS=Ctrl
(Tested on iMacros for FF v8.8.2, PM v26.3.3, Win10_x64.)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Is it possible to Search and Replace Text on Website?

Post by viking » Sun Jul 12, 2020 10:29 pm

I am so sorry, I had several trial versions of versions of EVENT Commands and pasted the wrong codes above.
In any case, I have problems reproducing your recordings. Let me take it step by step (I really appreciate your help in trying to figure this out!):

1. To only select all text, I start EVENT recording, click in the text box and click ctrl-a. The following is then recorded:

Code: Select all

URL GOTO=https://www.quantopian.com/algorithms/5f0a5f7cd986d33f1b283c81
EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV" BUTTON=0
Why wasn't also

Code: Select all

EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="a"  MODIFIERS=Ctrl
recorded?
I then decided to modify my recording by manually adding this line to my code (I removed the URL since not needed):

Code: Select all

EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV" BUTTON=0
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="a"  MODIFIERS=Ctrl
When I play this, nothing is selected! Why Not?

2. In the second trial, I start EVENT recording, click in the text box, click ctrl-a and then ctrl-c. The following is then recorded (Skipping the URL again and below):

Code: Select all

EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV>DIV>DIV:nth-of-type(5)>DIV>PRE" BUTTON=0
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="c"
The selection (ctrl-a) is still missing (and MODIFIERS=Ctrl is not recorded as I am aware). Playing this, after adding MODIFIERS=ctrl, still doesn't work.
I then again added the missing ctrl-a code (and MODIFIERS):

Code: Select all

EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV>DIV>DIV:nth-of-type(5)>DIV>PRE" BUTTON=0
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="a" MODIFIERS=Ctrl
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="c" MODIFIERS=Ctrl
This still doesn't select & copies the text. Why not?

3. OK, let's try chivracq's code for selecting and copying:

Code: Select all

EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHARS ="ac" MODIFIERS=Ctrl
This works (I don't even need the initial mouse click command)! Why does it work when "ac" is combined but not as separate commands? And, why can't I record this!?

4. Now lets try to select (mouse click + ctrl-a), copy (ctrl-c) and delete (ctrl-x). The following is recorded:

Code: Select all

EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV>DIV>DIV:nth-of-type(5)>DIV>PRE" BUTTON=0
EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHARS="cx"
Again, the selection (ctrl-a) is missing!? I also didn't get KEY=46 code. Maybe that is because you used the Delete button?
Try again using select (mouse click + ctrl-a), copy (ctrl-c) and delete ("Delete Button")). The following is recorded:

Code: Select all

EVENT TYPE=CLICK SELECTOR="#ide-container>DIV>DIV:nth-of-type(6)>DIV>DIV>DIV>DIV>DIV:nth-of-type(5)>DIV>PRE>SPAN>SPAN" BUTTON=0
EVENT TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHAR="c"
Now only the copy command is recorded!??

This is all very confusing! Did you just record the keystrokes: ctrl-a, ctrl-c, ctrlx (or "Delete") to get our code? I am obviously not able to record it properly :( :!: :?:

Again, thanks for all your help!
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Mon Jul 13, 2020 1:02 am

OK, I don't quote your (long) previous Post, I "trust" you won't delete/mutilate that Post... :wink:
(Main Reason I usually systematically quote is because many (New) Users often delete/mutilate their OP/Posts once they've got their Answer/Solution/Script working, usually to prevent any "Competition" from using "their" Script... :shock: )

Yeah, well, "why-why-why...?", ah-ah...! Only on the 'w3schools' Page did I do a little bit of Testing, to also find out that practically nothing would get recorded indeed in that Code Input Field, so I simply typed all the Commands/Statements from Scratch/Memory once I managed to identify the/a "correct" Selector, from previously doing some Testing for another Thread a bit recently about the "pain-in-the-ass" 'CodeMirror' Editor...

And I've been using the 'EVENT' since Day 1 it was implemented in v8.24 for FF, in March 2013, so that's already 7 years ago, and I actually came to the Forum 2 months later, in May 2013, to report a nasty Bug for the 'EVENT' Mode for which I couldn't find a Workaround at that time, ah-ah...! (The Bug is still there, but I finally found a Workaround a few years later, ah-ah...!)
But OK, so I guess I'm pretty "Advanced" with the 'EVENT' Mode, and I must probably have answered/posted in practically all Threads on the Forum related to that Mode, and done quite some Testing for all those Threads, so I usually "know" from the top of my Head, which Syntax to use, even if it (often) doesn't even get recorded, ah-ah...!

And on your 'quantopian' Site, I only recorded the following Statement, to check if you had the "correct" Identifier in your Script...:

Code: Select all

EVENTS TYPE=KEYPRESS SELECTOR="#ide-container>DIV>DIV>TEXTAREA" CHARS ="chivracq"
The Reason the 'Ctrl^a' + 'Ctrl^c' need to be combined in one Statement is because the Focus on the 'Select All' will be lost if you use 2 Statements, and the 'Copy' won't copy anything...

And hum, your Idea of using 'Ctrl^x' (= 'Cut') instead of 'Ctrl^c' is not a bad Idea actually, it seems to work manually, then the 'Del' or 'Backspace' wouldn't be needed, ah-ah...!
... And, yep it works, very good, ah-ah...!: :D

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
'URL GOTO=https://www.quantopian.com/algorithms/5f0a5f7cd986d33f1b283c81

SET Selector "#ide-container>DIV>DIV>TEXTAREA"

SET Search_Str "MAX_GROSS_EXPOSURE = 1.0"
SET Replace_Str "MAX_GROSS_EXPOSURE = 2.0"

'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR={{Selector}} CHARS="ax"  MODIFIERS=Ctrl

SET CB {{!CLIPBOARD}}
SET New_Code EVAL("var cb='{{CB}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=cb.split(ss).join(rs); z;")
SET !CLIPBOARD {{New_Code}}

EVENT TYPE=KEYPRESS SELECTOR={{Selector}} CHAR="v" MODIFIERS=Ctrl
(Tested on iMacros for FF v8.8.2, PM v26.3.3, Win10_x64.)

>>>

Alright, and I'm being "nice", ah-ah...!, but if you want to modify several Parameters, you can make your Script quite "generic" and add New Params with a simple Copy&Paste of a Block, and just need to modify one Number for the Code to keep working, stg like...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
'URL GOTO=https://www.quantopian.com/algorithms/5f0a5f7cd986d33f1b283c81

'Easy Access Vars:
SET Param_1 "MAX_GROSS_EXPOSURE = __1.0__2.0"
SET Param_2 "MAX_POSITION_CONCENTRATION = __0.001__0.003"
SET Param_3 "RETURNS_LOOKBACK_DAYS = __5__7"
'>
SET Selector "#ide-container>DIV>DIV>TEXTAREA"


'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR={{Selector}} CHARS="ax"  MODIFIERS=Ctrl
SET Orig_Code {{!CLIPBOARD}}
SET Code {{Orig_Code}}


'Modify Param_1:
SET Param {{Param_1}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET Code {{New_Code}}

'Modify Param_2:
SET Param {{Param_2}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET Code {{New_Code}}

'Modify Param_3:
SET Param {{Param_3}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET Code {{New_Code}}

'More Params...?
'SET Param {{Param_4}}
'=> Etc...


'Paste New Code:
SET !CLIPBOARD {{New_Code}}
EVENT TYPE=KEYPRESS SELECTOR={{Selector}} CHAR="v" MODIFIERS=Ctrl
And yep..., works nicely...! 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Is it possible to Search and Replace Text on Website?

Post by viking » Mon Jul 13, 2020 2:12 am

Thanks chivracq!
I will not post your long quote either. Strange that iMacros is not recording the "select all". Good that you knew the commands! I wish that there was list of all the commands and Key Codes.

I will go through, test and try to understand your second script shortly :) !

However, there is an issue with the current approach; After the parameters have been modified; there is no way to bring back the original. Quantopian unfortunately "auto-saves" and there is no way to turn it off. Thus, next time, the script will not find the original values.

Is there some way to search for the string:
MAX_GROSS_EXPOSURE = xxx
instead of
MAX_GROSS_EXPOSURE = 1.0

Where xxx are all the characters until the end of the line? The macro would then replace this with the new string as before.
TIA!
chivracq
Posts: 9425
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Is it possible to Search and Replace Text on Website?

Post by chivracq » Mon Jul 13, 2020 3:45 am

viking wrote:
Mon Jul 13, 2020 2:12 am
Thanks chivracq!
I will not post your long quote either. Strange that iMacros is not recording the "select all". Good that you knew the commands! I wish that there was list of all the commands and Key Codes.

I will go through, test and try to understand your second script shortly :) !

However, there is an issue with the current approach; After the parameters have been modified; there is no way to bring back the original. Quantopian unfortunately "auto-saves" and there is no way to turn it off. Thus, next time, the script will not find the original values.

Is there some way to search for the string:
MAX_GROSS_EXPOSURE = xxx
instead of
MAX_GROSS_EXPOSURE = 1.0

Where xxx are all the characters until the end of the line? The macro would then replace this with the new string as before.
TIA!

Yeah, well, not complicated to add some "Reverse" Functionality, as both Values are hard-coded in the Script, => just need to toggle the 'Reverse' Var in the 'Easy Access Vars' Section between 'YES'/'NO'...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1
'URL GOTO=https://www.quantopian.com/algorithms/5f0a5f7cd986d33f1b283c81

'Easy Access Vars:
SET Param_1 "MAX_GROSS_EXPOSURE = __1.0__2.0"
SET Param_2 "MAX_POSITION_CONCENTRATION = __0.001__0.003"
SET Param_3 "RETURNS_LOOKBACK_DAYS = __5__7"
'>
SET Reverse YES
SET Selector "#ide-container>DIV>DIV>TEXTAREA"


'Extract Original Content:
EVENTS TYPE=KEYPRESS SELECTOR={{Selector}} CHARS="ax"  MODIFIERS=Ctrl
SET Orig_Code {{!CLIPBOARD}}
SET Code {{Orig_Code}}


'Modify Param_1:
SET Param {{Param_1}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET New_Code_Rev EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(rs).join(ss); z;")
SET Code EVAL("var nc='{{New_Code}}', ncr='{{New_Code_Rev}}', r='{{Reverse}}'; var z; if(r=='YES'){z=ncr;} else{z=nc;}; z;")

'Modify Param_2:
SET Param {{Param_2}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET New_Code_Rev EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(rs).join(ss); z;")
SET Code EVAL("var nc='{{New_Code}}', ncr='{{New_Code_Rev}}', r='{{Reverse}}'; var z; if(r=='YES'){z=ncr;} else{z=nc;}; z;")

'Modify Param_3:
SET Param {{Param_3}}
SET Search_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[1]; z;")
SET Replace_Str EVAL("var p='{{Param}}'; var x,y,z; x=p.split('__'); z=x[0]+x[2]; z;")
'>
SET New_Code EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(ss).join(rs); z;")
SET New_Code_Rev EVAL("var c='{{Code}}', ss='{{Search_Str}}', rs='{{Replace_Str}}'; var z=c.split(rs).join(ss); z;")
SET Code EVAL("var nc='{{New_Code}}', ncr='{{New_Code_Rev}}', r='{{Reverse}}'; var z; if(r=='YES'){z=ncr;} else{z=nc;}; z;")


'Paste New Code:
SET !CLIPBOARD {{Code}}
EVENT TYPE=KEYPRESS SELECTOR={{Selector}} CHAR="v" MODIFIERS=Ctrl
(Tested on iMacros for FF v8.8.2, PM v26.3.3, Win10_x64.)

I also save the Original Code/Content to the 'Orig_Code' Var, and don't do anything with it, but the Purpose was indeed if you want to save that Original Code for example to your Local Drive, in case you want to revert it (manually)... Or you could also copy it to the Clipboard at the end of the Script... :idea:

And hum, to search for "MAX_GROSS_EXPOSURE = xxx", beh..., yep, can be done using 'match()' or 'search()', + 'REGEXP' for both, or even with 'replace()' + 'REGEXP', but I don't like 'REGEXP', ah-ah...!, Reason I prefer to use 'split().join()' instead of Global 'replace()'..., but 'split()' indeed needs the exact String... (and is also Case Sensitive)... :P
But I'm pretty sure it can be done solely with 'split()', until some next Space, or some "\n" or "\r" Char till the End of the Line, or the "#" that seems to come first after each Param in the Code..., hum except for the 3rd one..., then yep, would have to use the "\n" or "\r"... Would need to test because of the '\' Char which also happens to be the Escape Char... But I'm getting a bit tired now... :oops:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Is it possible to Search and Replace Text on Website?

Post by viking » Mon Jul 13, 2020 5:27 am

The "Reverse" Functionality" may not be the ideal way to do this, because I plan to test many different combinations of variables, and many different Algos. Furthermore, as soon as I press the "backtest" button, I leave the current page and will only return later.

I am very poor at RegEx (even though that may be the best solution?). I also don't know how to use split to get it to work...
Note that the variables that I listed in this example were just examples. They may look very different in different Algos. However, they will all be of the type "something"= something else. I would then always search for "something"= xxx (until end of line.
viking
Posts: 243
Joined: Sun Mar 16, 2008 7:22 am

Re: Is it possible to Search and Replace Text on Website?

Post by viking » Mon Jul 13, 2020 8:35 pm

This is starting to get outside the scope of iMacros now, but maybe you can help anyway...?
I found a RegExp that may work:

Code: Select all

 (?<=Parameter 1 =).*$

It supposedly finds all characters after "Parameter 1 =" until the end of the line.
I tested it here https://regex101.com/ using "Parameter 1 = 42 " as test string and the output was the desired " 42 ".
However, it didn't work when I tested it as a search/replace here https://www.w3schools.com/js/tryit.asp? ... ng_replace

I modified the code to:

Code: Select all

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Regular Expressions</h2>

<p>Replace "42" with "55" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Parameter 1 = 42</p>

<script>
function myFunction() {
  var str = document.getElementById("demo").innerHTML; 
  var txt = str.replace((?<=Parameter 1 =).*$,"55");
  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>
Nothing got replaced (Output = Input = Parameter 1 = 42). Is there a syntax error in my code on W3schools maybe...?
Post Reply