V
V
volverine2014-07-26 00:40:59
PHP
volverine, 2014-07-26 00:40:59

How to access the active tab from js?

I am making a plugin for Chrome, I ran into a problem:
There is a function in the script file (i.e. the script is not executed on the browser page, but somewhere in the background)

function onClickHandler(info, tab) {
    Some code;
};

Those. when a specific button of the plugin is pressed, this function is called and, in theory, the active window is transferred to tab, I checked through alert(tab.url), indeed it is transferred. It would be desirable to learn as having this data in this window to address to an element.
From the browser console it works fine
top.frames[0].document.getElementById("ElementID")
Naturally, if you write it in a script, it will not work, I tried it like this
tab.top.frames[0].document.getElementById("ElementID")
so
chrome.tab.top.frames[0].document.getElementById("ElementID")
so
chrome.tabs.top.frames[0].document.getElementById("ElementID")

Unfortunately, nothing happened, I didn’t really find any information, please help, I just started learning js.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PrAw, 2016-04-25
@leha78

1. It makes no sense to log in for each post. Move the block with authorization above immediately after if ($postsToUp), before foreach
2. For debugging, it makes sense to save the received pages, plus preferably with the time when it was received.
3. Move all gestures with curl into a separate function, readability will jump sharply
4. The fence of closing curly braces hints that too much has been stuffed into one function, it's time to break it into separate ones.
Perhaps the error occurs from the server side, so save all the pages and see, after which it stopped working. I don't see any error handling. The only place where something can go wrong is if (!empty($cookies['member_id'])) - if no cookies are found, it silently exits. At least tell me why

A
Alexander Kovpashko, 2014-07-26
@volverine

It looks like you need to use ContentScripts to communicate between the browser page and the plugin's background page. More details here https://developer.chrome.com/extensions/content_scripts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question