Merge pull request #931 from atom/will-navigate
Add "will-navigate" event
This commit is contained in:
commit
c01e3cf9aa
9 changed files with 61 additions and 5 deletions
|
@ -111,9 +111,15 @@ content::WebContents* WebContents::OpenURLFromTab(
|
|||
args.AppendString("");
|
||||
args.AppendInteger(params.disposition);
|
||||
Emit("-new-window", args);
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Give user a chance to cancel navigation.
|
||||
base::ListValue args;
|
||||
args.AppendString(params.url.spec());
|
||||
if (Emit("will-navigate", args))
|
||||
return nullptr;
|
||||
|
||||
content::NavigationController::LoadURLParams load_url_params(params.url);
|
||||
load_url_params.referrer = params.referrer;
|
||||
load_url_params.transition_type = params.transition;
|
||||
|
|
|
@ -92,6 +92,12 @@ void Window::WillCreatePopupWindow(const base::string16& frame_name,
|
|||
Emit("-new-window", args);
|
||||
}
|
||||
|
||||
void Window::WillNavigate(bool* prevent_default, const GURL& url) {
|
||||
base::ListValue args;
|
||||
args.AppendString(url.spec());
|
||||
*prevent_default = Emit("-will-navigate", args);
|
||||
}
|
||||
|
||||
void Window::WillCloseWindow(bool* prevent_default) {
|
||||
*prevent_default = Emit("close");
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ class Window : public mate::EventEmitter,
|
|||
const GURL& target_url,
|
||||
const std::string& partition_id,
|
||||
WindowOpenDisposition disposition) override;
|
||||
void WillNavigate(bool* prevent_default, const GURL& url) override;
|
||||
void WillCloseWindow(bool* prevent_default) override;
|
||||
void OnWindowClosed() override;
|
||||
void OnWindowBlur() override;
|
||||
|
|
|
@ -30,6 +30,10 @@ BrowserWindow::_init = ->
|
|||
options = show: true, width: 800, height: 600
|
||||
ipc.emit 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', event, url, frameName, options
|
||||
|
||||
# Redirect "will-navigate" to webContents.
|
||||
@on '-will-navigate', (event, url) =>
|
||||
@webContents.emit 'will-navigate', event, url
|
||||
|
||||
# Remove the window from weak map immediately when it's destroyed, since we
|
||||
# could be iterating windows before GC happened.
|
||||
@once 'closed', =>
|
||||
|
|
|
@ -169,7 +169,7 @@ NativeWindow* NativeWindow::FromRenderView(int process_id, int routing_id) {
|
|||
return window;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
|
||||
|
@ -370,13 +370,13 @@ void NativeWindow::CloseWebContents() {
|
|||
|
||||
content::WebContents* NativeWindow::GetWebContents() const {
|
||||
if (!inspectable_web_contents_)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
return inspectable_web_contents()->GetWebContents();
|
||||
}
|
||||
|
||||
content::WebContents* NativeWindow::GetDevToolsWebContents() const {
|
||||
if (!inspectable_web_contents_)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
return inspectable_web_contents()->devtools_web_contents();
|
||||
}
|
||||
|
||||
|
@ -524,9 +524,17 @@ content::WebContents* NativeWindow::OpenURLFromTab(
|
|||
params.url,
|
||||
"",
|
||||
params.disposition));
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Give user a chance to prevent navigation.
|
||||
bool prevent_default = false;
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver,
|
||||
observers_,
|
||||
WillNavigate(&prevent_default, params.url));
|
||||
if (prevent_default)
|
||||
return nullptr;
|
||||
|
||||
content::NavigationController::LoadURLParams load_url_params(params.url);
|
||||
load_url_params.referrer = params.referrer;
|
||||
load_url_params.transition_type = params.transition;
|
||||
|
|
|
@ -27,6 +27,9 @@ class NativeWindowObserver {
|
|||
const std::string& partition_id,
|
||||
WindowOpenDisposition disposition) {}
|
||||
|
||||
// Called when user is starting an navigation in web page.
|
||||
virtual void WillNavigate(bool* prevent_default, const GURL& url) {}
|
||||
|
||||
// Called when the window is gonna closed.
|
||||
virtual void WillCloseWindow(bool* prevent_default) {}
|
||||
|
||||
|
|
|
@ -616,6 +616,19 @@ will be returned to `window.open` to let you have limited control of it.
|
|||
|
||||
Calling `event.preventDefault()` can prevent creating new windows.
|
||||
|
||||
### Event: 'will-naviagte'
|
||||
|
||||
* `event` Event
|
||||
* `url` String
|
||||
|
||||
Emitted when user or the page wants to start an navigation, it can happen when
|
||||
`window.location` object is changed or user clicks a link in the page.
|
||||
|
||||
This event will not emit when the navigation is started programmely with APIs
|
||||
like `WebContents.loadUrl` and `WebContents.back`.
|
||||
|
||||
Calling `event.preventDefault()` can prevent the navigation.
|
||||
|
||||
### Event: 'crashed'
|
||||
|
||||
Emitted when the renderer process is crashed.
|
||||
|
|
|
@ -208,3 +208,11 @@ describe 'browser-window module', ->
|
|||
w.once 'minimize', -> done()
|
||||
w.show()
|
||||
w.minimize()
|
||||
|
||||
describe 'will-navigate event', ->
|
||||
it 'emits when user starts a navigation', (done) ->
|
||||
w.webContents.on 'will-navigate', (event, url) ->
|
||||
event.preventDefault()
|
||||
assert.equal url, 'https://www.github.com/'
|
||||
done()
|
||||
w.loadUrl "file://#{fixtures}/pages/will-navigate.html"
|
||||
|
|
7
spec/fixtures/pages/will-navigate.html
vendored
Normal file
7
spec/fixtures/pages/will-navigate.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
location = 'https://www.github.com'
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue