0

I have a java script/chrome extension that simulates clicks from a commercial web page, that works fine in the chrome console. But when I run it from the extension, I get "onUpdate is not a function", which is referring to a callback (onclick="return onUpdate();) referenced in the popped up page html.

Popped up page html

<div id="divButtons" align="left" style="position: relative; z-index: 10;">
<table border="0" cellspacing="0" cellpadding="0" width="97%" summary="">
    <tbody><tr>
        <td class="swlErrorMessage" id="errorStatus">Ready</td>
    </tr>
    <tr>
        <td><font size="1">&nbsp;</font></td>
    </tr>
    <tr align="right">
        <td>
            <input type="button" class="button" value="OK" name="ok" onclick="return onUpdate();" title=""> &nbsp;
            <input type="button" class="button" value="Cancel" name="cancel" onclick="return onCancel();" title="">
        </td>
    </tr>
</tbody></table>

I think I figured out where the break is. Basically, if I run the code in console at once, it works fine. But if I manually click on the link and then use the chrome extension to target the window, it performs everything correctly other than the save, and I noticed in my code, when I run the window.open and target the name, that seems to break any reference back to the main page and it's like it doesn't have any reference to any of the previous callbacks. If I open the chrome console on the page that was popped up, run the window.open again, and execute the code, it all works.

Not sure why everything but this one function isn't working. onUpdate().

I have ran it from the console and it works fine.

code.js

function cfs_all(){
var x = document.getElementById('tabFrame');
var y = x.contentDocument ;
var link = y.getElementById('urlListTable');
link = link.getElementsByTagName('tr');
runLoop = async() => {
for(a=0;a<link.length;a++){
    await new Promise(resolve => setTimeout(resolve, 2000))
    if (link[a].children[1].innerText.includes('Allow') == true){
    console.log('Yesssssssssss '+ String(a))
    var rest = y.getElementsByTagName('tr')
    rest = rest[a+2].getElementsByTagName('td')
            //simulates the button click to open the pop up window
    rest[3].getElementsByTagName('img')[0].click()
    setTimeout(function(){
    //connects to the popped up window
    var test;
    var text = "*.test.edu"
    test = String(window.origin)
    test = test.substring(8,)
    test = test.replace(/\:.*/,'');
    test = test.split('.')
            //opens the window by referencing the window.name
    var existingWin = window.open('', test[0]+ "_" + test[1] + "_" + test[2] + "_" + test[3] + "_urlObjDlg");
    console.log(test[0]+ "." + test[1] + "." + test[2] + "." + test[3])
            //clicks add, opens textbox
    existingWin.document.getElementsByClassName('button')[0].click()
            //adds the value to the text box
    existingWin.document.getElementsByTagName('input')[9].value = text
            //clicks save button
    try{
    existingWin.document.getElementsByClassName('button roundBtn swlEventSave')[0].click()
    }
    catch(err){
    existingWin.document.getElementsByClassName('button roundBtn swlEventClose')[0].click()
    }
            //updates the page, THIS IS WHAT's NOT WORKING!!!
    existingWin.onUpdate;   
    }, 1500);
    }
    else
    {
    console.log('Nooooooo ' + String(a))
    }
    }
}
runLoop()
    }
cfs_all()

I can paste the above code in and it works in the console

this is the code I have to paste in the popped up window's chrome console, and it works...

function cfs_add(){

var test;
test = String(window.origin)
test = test.substring(8,)
test = test.replace(/\:.*/,'');
test = test.split('.')
var existingWin = window.open('', test[0]+ "_" + test[1] + "_" + test[2] + 
"_" + test[3] + "_urlObjDlg");
console.log(existingWin)
existingWin.onUpdate()

}
cfs_add()

It's the same code...

Jason Owens
  • 525
  • 2
  • 6
  • 21
  • 1) `existingWin.onUpdate;` should be `existingWin.onUpdate();` to actually call the function, 2) But you can't call web page functions directly anyway, you'll need an additional content script in your manifest.json that matches the opened window's URL and inside that content script you can run `existingWin.onUpdate()` as [a page script code](/a/9517879). – wOxxOm Sep 04 '19 at 06:06
  • 1) that was a typo but thanks. 2) when you say "Opened window's url", is this the popped up child, or the parent page? – Jason Owens Sep 04 '19 at 15:43
  • In the window where that functions is. I guess it's the child. – wOxxOm Sep 04 '19 at 15:50
  • Here's the weird thing, while the popup is still open, I don't have access to the buttons functionality. Meaning, that if I click on the popup, all buttons work, up until I issue the window.open command to reference that popup to enter the info and click save. It finds the dom elements so that they can import into the text box, it's just this one that is using the return onUpdate(). So, once I reference the popup with the window.open, I can no longer use the "ok" or "cancel" buttons on the popup. I have access to it up until this point. after window.open, clicking on buttons dont work. – Jason Owens Sep 04 '19 at 16:33

0 Answers0