I'm trying to hack a web application that I don't own but that I wish to improve.
I'd like to add some HTML before an element in the DOM, of a same-domain iframe. Here's a sample from the console:
jQ(".x-panel-body.x-panel-body-noheader").find("iframe").contents().find("body").find("#WorkspaceFrame").find(".x-panel-tbar.x-panel-tbar-noheader").find(".x-toolbar-right").find(".x-toolbar-right-ct").find("table");
[
<table cellspacing="0">…</table>,
<table id="ext-comp-1046" cellspacing="0" class="x-btn x-btn-text-icon" style="width: auto;">…</table>,
<table cellspacing="0">…</table>,
<table cellspacing="0">…</table>,
<table id="ext-comp-1067" cellspacing="0" class="x-btn x-btn-text-icon " style="width: auto;">…</table>,
<table id="ext-comp-1068" cellspacing="0" class="x-btn x-btn-text-icon " style="width: auto;">…</table>,
<table id="ext-comp-1069" cellspacing="0" class="x-btn x-btn-text-icon" style="width: auto;">…</table>,
<table cellspacing="0">…</table>,
<table cellspacing="0">…</table>,
<table id="ext-comp-1253" cellspacing="0" class="x-btn x-btn-text-icon x-item-disabled" style="width: auto;">…</table>,
<table id="ext-comp-1254" cellspacing="0" class="x-btn x-btn-text-icon" style="width: auto;">…</table>,
<table cellspacing="0">…</table>,
<table cellspacing="0">…</table>,
<table id="ext-comp-1760" cellspacing="0" class="x-btn x-btn-text-icon" style="width: auto;">…</table>,
<table cellspacing="0">…</table>
]
jQ is the name of my jQuery (I can't use $).
So basically it works from the console, and saveButton contains an array. Here's my script:
saveButton=jQ(".x-panel-body.x-panel-body-noheader").find("iframe").contents().find("body").find("#WorkspaceFrame").find(".x-panel-tbar.x-panel-tbar-noheader").find(".x-toolbar-right").find(".x-toolbar-right-ct").find("table");
console.log(saveButton);
jQ.each(saveButton, new function(i,v) { console.log("#"+i+": "+v); });
Here's the console output:
[prevObject: b.fn.b.init[0], context: document, selector: "body #WorkspaceFrame .x-panel-tbar.x-panel-tbar-noheader .x-toolbar-right .x-toolbar-right-ct table", jquery: "1.9.1", constructor: function…]
#undefined: undefined
So saveButton is an object, but not an array, and this is confirmed by the failed jQ.each.
I'd like to take one of the tables (it's always the 3rd) and call .before('<p>something</p>') on it. I tried saveButton[2].before('<p>HI</p>'), it's not working.
Here's a new try after the hint in Zoltan's answer:
jQ(".x-panel-body.x-panel-body-noheader").find("iframe").contents().find("body").find("#WorkspaceFrame").find(".x-panel-tbar.x-panel-tbar-noheader").find(".x-toolbar-right").find(".x-toolbar-right-ct").find("table").eq(8).find(">:first-child").find(">:first-child").find(">:first-child").before('<td class="x-toolbar-cell"><button>Test</button></td>');
Typing this in the console properly adds the button where I want it, however the same line in the JS code isn't working. What gives?