0

I am able to use messaging to have a content script communicate with background.js:

content.js

chrome.runtime.sendMessage({type: 'injectTheAdditionalScript'});

Background.js

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse){
    console.log('In Background and received message from iFrame...' + sender.tab.id + ' / ' + sender.frameId);
    if(typeof message === 'object' && message.type === 'injectTheAdditionalScript') {
        chrome.tabs.executeScript(sender.tab.id,{
          frameId: sender.frameId,
          code: "console.log('Injecting a console log...');\
                  console.log('2nd line injecting a console log...');\
                  var activeEl = document.activeElement;\
                  var activeTag = activeEl.tagName.toLowerCase();\
                  var activeID = activeEl.id;\
                  console.log('Injected code and type of element is: ' + activeEl.id + ' / ' + activeTag);\
                "
        });

    }
});

The console.log messages successfully show in the page with the content script.

But I need to know the frameID in order to append/add html to the iframe.

My goal is to 'paste' a string into the iframe - as if you'd CTRL-V'd a string into the iframe.

It would seem that you'd be able to 'get' the element ID of the iframe if you can run code in it...but the 'activeID' variable in the code above is blank.

Thank you for any consideration/help.

11teenth
  • 1,853
  • 1
  • 15
  • 28
  • I'm still not sure why you need the `frameId` beyond its use for injecting the script. If the point is to get the `sender.frameId` to the content script, you can do that with message passing, or [another way](http://stackoverflow.com/questions/17567624/pass-a-parameter-to-a-content-script-injected-using-chrome-tabs-executescript/40815514#40815514). Where do you want the HTML appended? Would [`.insertAdjacentHTML()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML) work for what you want? `activeID` may be blank because the element has no ID, which is valid. – Makyen Apr 21 '17 at 03:47
  • I shouldn't presume to guess at the best direction...I want the HTML appended to the text in the iframe (specific use case is a Google Doc). Where do you attach the insertAdjacentHTML if you don't have the Element ID? – 11teenth Apr 21 '17 at 04:08
  • You need the element, not the element's ID. Not all elements have IDs. An element only has an ID if one has specifically been assigned in the HTML, or by JavaScript. – Makyen Apr 21 '17 at 04:13
  • Thanks...my specific use case is Google Docs, which I think is far too complex to accomplish what I want. Although it does seem that you could publish a public Google Apps Script and then access it from your Google Extension to insert text into a Google document... – 11teenth Apr 21 '17 at 13:58

0 Answers0