I'm developing a Chrome Extension for a specific Web Page that hides some elements of the page to avoid using some functions of this and put it in a Kiosk, simple no?.
The thing is, the page is not "fast enough" so when the extension do his work (on document ready) there's some seconds showing all the webpage.
I'm trying to show a loader when the page is still loading and when the user click on some of the allowed links but with no success.
The thing i'm trying is:
Catch before request event and inject the Loader:
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.executeScript(activeTab.id, {
file: 'injectLoader.js',
runAt: 'document_start'
}, function(){
});
});
}, { urls: ["*://*/*URL*"] }
);
The injectLoader.js is something like:
var div = document.createElement('div');
div.id = 'sstechLoader';
div.innerHTML = '<h1>CARGANDO</h1>';
document.body.appendChild(div)
Then i try to delete the loader div in the completed event:
chrome.webRequest.onCompleted.addListener(
function (details) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.executeScript(activeTab.id, {
code: "setTimeout(function(){document.querySelector('#sstechLoader').remove()},1000);",
runAt: 'document_end'
}, function(){
});
});
}, { urls: ["*://*/*URL*"] }
);
(i added a timeout just in case)
The thing is, it's not working, the loader appears but the site can be saw for seconds. And the loader never disappear i don't know why the onCompleted is never called.
There's some other way to accomplish what i'm trying to do?
UPDATED
I added the relevant code in request of @woxxom and @sam
UPDATE With solution
After the @Makyen suggestion i just found the solution putting the put loader logic in the chrome.webNavigation.onCommitted
:
chrome.webNavigation.onCommitted.addListener(function(details){
chrome.tabs.executeScript(details.tabId, {
file: 'injectLoader.js',
runAt: 'document_start'
}, function(){
});
});
Thanks a lot for your help guys !.