-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render web pages including Single Page Application such as React. resolves #4
- Loading branch information
1 parent
3a33f50
commit 879b46d
Showing
7 changed files
with
254 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package browser | ||
|
||
import ( | ||
"context" | ||
// "urlgrab/utilities" | ||
|
||
"github.com/chromedp/chromedp" | ||
) | ||
|
||
var GlobalContext context.Context | ||
var GlobalCancel context.CancelFunc | ||
|
||
func GetRenderedSource(url string) string { | ||
|
||
// same browser, second tab | ||
newCtx, newCtxCancel := chromedp.NewContext(GlobalContext) | ||
defer newCtxCancel() | ||
|
||
// ensure the second tab is created | ||
if err := chromedp.Run(newCtx); err != nil { | ||
newCtxCancel() | ||
// utilities.Logger.Fatal(err) | ||
} | ||
|
||
// navigate to a page, and get it's entire HTML | ||
var outerHtml string | ||
|
||
if err := chromedp.Run(newCtx, | ||
chromedp.Navigate(url), | ||
chromedp.OuterHTML("html", &outerHtml), | ||
); err != nil { | ||
// utilities.Logger.Error(err) | ||
} | ||
|
||
return outerHtml | ||
} | ||
|
||
func GetGlobalContext(headless bool, proxy string) (context.Context, context.CancelFunc) { | ||
var ( | ||
allocCtx context.Context | ||
cancel context.CancelFunc | ||
) | ||
if proxy == "" { | ||
allocCtx, cancel = chromedp.NewExecAllocator(context.Background(), | ||
chromedp.Flag("headless", headless), | ||
chromedp.Flag("ignore-certificate-errors", true), | ||
chromedp.Flag("disable-extensions", true), | ||
chromedp.Flag("no-first-run", true), | ||
chromedp.Flag("no-default-browser-check", true), | ||
) | ||
} else { | ||
allocCtx, cancel = chromedp.NewExecAllocator(context.Background(), | ||
chromedp.Flag("headless", headless), | ||
chromedp.Flag("ignore-certificate-errors", true), | ||
chromedp.Flag("disable-extensions", true), | ||
chromedp.Flag("no-first-run", true), | ||
chromedp.Flag("no-default-browser-check", true), | ||
chromedp.Flag("no-default-browser-check", true), | ||
chromedp.Flag("proxy-server", proxy), | ||
) | ||
} | ||
|
||
// create chrome instance | ||
ctx, cancel := chromedp.NewContext(allocCtx, | ||
// chromedp.WithErrorf(utilities.Logger.Errorf), | ||
chromedp.WithBrowserOption(), | ||
) | ||
|
||
// ensure the first tab is created | ||
if err := chromedp.Run(ctx); err != nil { | ||
// utilities.Logger.Fatal(err) | ||
} | ||
|
||
return ctx, cancel | ||
} |
Oops, something went wrong.