Chrome Extension detection tab fully loaded

I’m creating a Chrome extension, part of its function is to click a link of a website and the site itself will open a new tab, then my extension will print the tab. I’m now facing a problem in detecting the new tab’s completeness.

My original approach is to use chrome.tabs.onUpdated and by detecting the status change to complete, the code will continue to run.

async function asyncChkTabLoaded(targetTabID){
    return new Promise(resolve => {
        chkTabLoaded = function (tabID, tabChanges, tabData) {
        if (tabID == targetTabID) {
            if ("status" in tabChanges) {
                if (tabChanges.status == "complete"){
                    resolve(tabData);
                }
            }
        }
    }
    chrome.tabs.onUpdated.addListener(chkTabLoaded);
    });
}
await asyncChkTabLoaded(openedTabID);

However, I found that by using this approach, part of the content is not fully loaded, mainly the images. And so the auto-print following will print a website with empty images. complete is not really meaning complete.

I believe this is due to the weird properties of the tabs.onUpdated that “The status property will cycle through "loading" and "complete".” (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onUpdated)

I know that I can detect the loading status of the image by using image.complete. However, some of the image is a <image> tag wrapping in a <svg>, for example:

<svg id="main-svg" preserveAspectRatio="xMidYMid" viewBox="0 0 486 934">

<image x="0.00" y="0.00" width="486.69" height="632.84" image-rendering="-moz-crisp-edges" preserveAspectRatio="none" xlink:href="data:image/png;base64, iVB0KGgoAA..P2YII="></image>

</svg>

And it seems there’s no complete attribute for svg element. So using image.complete may not fully fit my case.

So I think it is the most safetest way to use the load event of the tab window as a guideline to detect the completeness of the website contents. And here’s how I modify the asyncChkTabLoaded

async function asyncChkTabLoaded(){
    return new Promise(resolve => {
        console.log("injected onload");
        console.log(document.readyState);
        sendResolve = function() {
            console.log("Window Loaded");
            resolve();
        }
        window.addEventListener("load", sendResolve);
        
    });
}
await chrome.scripting.executeScript({target : {tabId : openedTabID}, func : asyncChkTabLoaded});

However it seems like the load event will never be fired, and so the promise will never be resolved and await forever, and my code will never proceed to the next step to print.
The document.readyState is always loading. The Window Loaded can only be printed if I move the resolve() out of the function sendResolve.

So my question is how can I detect the tab load event of the new tab? I prefer to use resolve in the load event, and await the resolve, instead of putting all the follow codes into the load eventlistener callback. So my code’s readability is easier and it’s easier to add further code after the await.

Or how can I detect the last tab status change of tabs.onUpdated?

Or how can I detect the loading of SVG element? But that will be the least preferable way to solve this problem, as I am not sure what other unpredictable elements are still loading.

Thank you very much for your help 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật