I am using Safari on the latest version of MacOS (12.3)
I am new to Jacascript and am poking around to learn some things. I am trying to activate a link on a webpage. To determine the button id I right clicked on the link and clicked inspect element. The highlighted element is as follows:
<a href="javascript:IDS_LinkButtonClick('M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01', "", false, false, false, '', 0, '', '');">long term</a> = $0
I think the button id is 'M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01'
Using the shortcuts app I made a one action shortcut using the action "Run Javascript On Active Safari Tab"
the code inside is:
document.getElementById('M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01').click();
completion(result);
Running this action yields the error "unable to run javascript on webpage please ensure the allow java script from apple events is enabled in the develop menu of safari." I have verified - multiple times - that both allow apple events in develop menu and allow javascript in preferences tab are checked. to be sure the I verified javascript does work by running the default code that comes in the shortcut action which did run:
var result = [];
// Get all links from the page
var elements = document.querySelectorAll("a");
for (let element of elements) {
result.push({
"url": element.href,
"text": element.innerText
});
}
// Call completion to finish
completion(result);
Any help/pointers would be appreciated. It must be that my interpretation of the element is wrong and javascript can't run because there is no link with that id but don't know how else to interpret it. thank you
CodePudding user response:
I think the button id is 'M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01'
That is not it. Therefore document.getElementById('M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01')
yields no element, thus calling .click()
on it will cause an Error.
Since the a
directly calls a JS function you can simply just call the same yourself as:
IDS_LinkButtonClick('M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01', "", false, false, false, '', 0, '', '');
The unable to run javascript on webpage issue is a separate one. It may be a bad message due to the missing ID causing a JS issue. If other JS code runs fine you should assume the JS giving the error is wrong.
Best way to test it is to paste it into the console in the Developer tools sidebar. Click "Show JavaScript Console" in the Develop menu to open it.
CodePudding user response:
What you thought was the is is nothing but the
href
value
Using the second method you wrote you can get the exact element you are looking for
// Get all links from the page
var elements = document.querySelectorAll("a");
for (let element of elements) {
if(element.href==="javascript:IDS_LinkButtonClick('M$layout$content$PCDZ$MXWGDOM$ctl00$Accounts$ctl02$ctl01', "", false, false, false, '', 0, '', '');" && element.innerText==="long term"){
// Do your desired work on the particular element
console.log(element)
}
}
// Call completion to finish