0

I have a chrome extension where the page is reloaded from inside a content script. The content script is being run using chrome.tabs.executeScript() from my popup.js on the currently active tab.

After the reload, I am unable to bind to the scroll event of the window from the content script. Is there a way to recapture the reloaded DOM to bind to the scroll event ?

Update:

popup.js

chrome.tabs.executeScript(null, { file: 'sendArticleLength.js', allFrames: true}, function(result) {
    count = result;                 
    chrome.tabs.reload(targetTabID, function() {
    chrome.tabs.executeScript({file: 'sendRemainingTime.js', allFrames: true}, function(){                  
            chrome.tabs.sendMessage(targetTabID, {wordcount:count});
    });
});

sendRemainingTime.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    if(request.wordcount) {
        wc = request.wordcount;
        window.onscroll = function(){
            alert('scrolled!');
        }
    }
});

In sendRemainingTime.js, I want to bind to onscroll event after the reload triggered in popup.js finishes. But currently, the onscroll binds before the reload.

0

1 Answer 1

1

Update:

As the debugging showed the real issue was something else :-) namely, usage of a var location = assignment which actually reloaded the page and the fact that chrome.tabs.reload executes its callback immediately without waiting for the page to actually reload, so chrome.tabs.onUpdated listener solved the issue.

Sign up to request clarification or add additional context in comments.

6 Comments

What should be sent via sendMessage ? I want to listen for the window scroll event on the reloaded page, but currently it is binding before the page reloads despite using a callback of chrome.tabs.reload(<tabid>,callback)
It's not important what you send, for example "reinject". If you need specific help with code then edit the question and add the code you have now.
Despite the claim reloaded from inside a content script it's actually reloaded from the popup script. So you only need to extend the permissions.
My permissions include http://*/* and https://*/*. So the extension should have permissions for the (http) web page I'm trying.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.