Run script in specific frame

I can only see how to execute the script in a top-level frame or in all frames on a specific tab:

chrome.tabs.executeScript(integer tabId, object details, function callback) 

Where, if details.allFrames is true , then it will be executed in each subframe, but if it is false, it will be executed only in the top-level frame. How can I provide a frameId to execute a script in?

+6
source share
4 answers

As far as I know, you cannot. Instead, set allFrames: true and write javascript in the contents of the script to determine if it is the correct frame, and return without any action if it is not the correct frame.

+5
source

This is the route I decided to solve the same problem:

https://github.com/Rob--W/chrome-api/blob/master/chrome.tabs.executeScriptInFrame/chrome.tabs.executeScriptInFrame.js

 function alertCookie(tabId, frameId) { chrome.tabs.executeScriptInFrame(tabId, { frameId: frameId, code: '// This code runs in one frame, specified via frameId \n' + 'alert(location.href);' + 'document.cookie;' }, function(results) { if (!results) { alert('Failed to execute code. See background page for details.'); return; } var cookie = results[0]; alert('Found cookie: ' + cookie); }); 

}

+2
source

From the documentation

If allFrames is true, it means that JavaScript or CSS should be entered in all frames of the current page. By default, it is false and is entered only in the top frame. If set to true and frameId, the code is inserted into the selected frame and all its child frames.

+2
source

Starting with Chrome 39, there is an optional parameter named frameId .

0
source

Source: https://habr.com/ru/post/919055/


All Articles