I developed a toolbar for Google Chrome by adding it to the pages through 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) within that iFrame (logic already implemented). I therefore need the 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 the content-script recognize the existence of a span within the 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?