Browse Source

fix: print warning after DOM is created

Cheng Zhao 6 years ago
parent
commit
90fc709194

+ 11 - 2
atom/browser/api/atom_api_web_contents.cc

@@ -310,7 +310,8 @@ WebContents::WebContents(v8::Isolate* isolate,
       type_(type),
       request_id_(0),
       background_throttling_(true),
-      enable_devtools_(true) {
+      enable_devtools_(true),
+      is_dom_ready_(false) {
   const mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
   if (type == REMOTE) {
     web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
@@ -799,8 +800,10 @@ void WebContents::DidChangeThemeColor(SkColor theme_color) {
 
 void WebContents::DocumentLoadedInFrame(
     content::RenderFrameHost* render_frame_host) {
-  if (!render_frame_host->GetParent())
+  if (!render_frame_host->GetParent()) {
+    is_dom_ready_ = true;
     Emit("dom-ready");
+  }
 }
 
 void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
@@ -822,6 +825,7 @@ void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
 }
 
 void WebContents::DidStartLoading() {
+  is_dom_ready_ = false;
   Emit("did-start-loading");
 }
 
@@ -1343,6 +1347,10 @@ bool WebContents::IsAudioMuted() {
   return web_contents()->IsAudioMuted();
 }
 
+bool WebContents::IsDOMReady() const {
+  return is_dom_ready_;
+}
+
 void WebContents::Print(mate::Arguments* args) {
   PrintSettings settings = { false, false, base::string16() };
   if (args->Length() >= 1 && !args->GetNext(&settings)) {
@@ -1934,6 +1942,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
                  &WebContents::SetIgnoreMenuShortcuts)
       .SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
       .SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
+      .SetMethod("isDomReady", &WebContents::IsDOMReady)
       .SetMethod("undo", &WebContents::Undo)
       .SetMethod("redo", &WebContents::Redo)
       .SetMethod("cut", &WebContents::Cut)

+ 4 - 0
atom/browser/api/atom_api_web_contents.h

@@ -125,6 +125,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void SetIgnoreMenuShortcuts(bool ignore);
   void SetAudioMuted(bool muted);
   bool IsAudioMuted();
+  bool IsDOMReady() const;
   void Print(mate::Arguments* args);
   std::vector<printing::PrinterBasicInfo> GetPrinterList();
   void SetEmbedder(const WebContents* embedder);
@@ -430,6 +431,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
   // Whether to enable devtools.
   bool enable_devtools_;
 
+  // Whether page's document is ready.
+  bool is_dom_ready_;
+
   DISALLOW_COPY_AND_ASSIGN(WebContents);
 };
 

+ 7 - 1
lib/browser/api/browser-window.js

@@ -55,7 +55,13 @@ BrowserWindow.prototype._init = function () {
         '"nativeWindowOpen" option will cause memory leaks, please turn off ' +
         'the "nodeIntegration" option.\\n' +
         'See https://github.com/electron/electron/pull/15076 for more.'
-      this.webContents.executeJavaScript(`console.warn('${message}')`)
+      // console is only available after DOM is created.
+      const printWarning = () => this.webContents.executeJavaScript(`console.warn('${message}')`)
+      if (this.webContents.isDomReady()) {
+        printWarning()
+      } else {
+        this.webContents.once('dom-ready', printWarning)
+      }
     }
 
     let {url, frameName} = urlFrameName