3
I developed a Toolbar for Google Chrome, adding it to the pages via an iframe:
var iframe = document.createElement('iframe');
iframe.id="iframeId";
iframe.src = chrome.extension.getURL('CLAWS_Sem_Imagens.html');
iframe.style.height = 7em;
iframe.style.width = '100%';
iframe.style.position = 'fixed';
iframe.style.top = '0';
iframe.style.left = '0';
iframe.style.zIndex = '938089';
//other stuff
document.documentElement.insertBefore(iframe,document.body);
// pulling down the rest of the page
var bodyStyle = document.body.style;
var cssTransform = 'transform' in bodyStyle ? 'transform' : 'webkitTransform';
bodyStyle[cssTransform] = 'translateY(' + height + ')';
What I need is that when the user selects a word on the main page, it is stored in a variable (or HTML element) that is within this iframe (logic already implemented). Therefore, I need content-script and Toolbar to communicate (the first recognizes that the word has been selected and stores it in a variable/element of the second).
For testing purposes, I’m trying to make content-script recognize the existence of a span within iframe:
<span id="palavra">
Palavra
</span>
I’ve tried to use iframe.contentDocument.getElementById("palavra")
, window.document.getElementById("iframeId").contentWindow.querySelector("#palavra")
and window.frames['iframeId'].contentDocument.getElementById('palavra')
, but nothing works. Any suggestions?
Thank you so much for answering! In fact, your code will be very useful to me! But...my main problem is not exactly this, but the part that you commented "arrow content in your variable". The problem is that I’m in a content_script, and my variable is inside an iframe that I add to the pages (through this content script), and I’m not able to access the internal content of this iframe. I will put a rough drawing in the post to try to explain better.
– Mr Guliarte
@Mrguliarte now understood your problem hehe .. coming home I try something to help you :DD
– Brunno
Man, I can’t thank you enough!
– Mr Guliarte
@Mrguliarte you came to see this ? http://stackoverflow.com/questions/11325415/access-iframe-content-from-a-chromes-extension-content-script is in English, if you have any problems let me know what abstraction here for you :D
– Brunno
Dude, I saw this link yesterday! Bad not have answered before! But it solved my problem yes, vlw : D
– Mr Guliarte