Merge pull request #3975 from leethomas/add-webview-navigation-events

Add webview navigation events
This commit is contained in:
Cheng Zhao 2016-01-04 11:24:27 +08:00
Родитель cf09e7cb51 ce733e5927
Коммит 941232a76b
10 изменённых файлов: 133 добавлений и 1 удалений

Просмотреть файл

@ -566,7 +566,9 @@ void WebContents::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
if (details.is_navigation_to_different_page())
Emit("did-navigate-to-different-page");
Emit("did-navigate-to-different-page", params.url);
else if (details.is_in_page)
Emit("did-navigate-in-page", params.url);
}
void WebContents::TitleWasSet(content::NavigationEntry* entry,

Просмотреть файл

@ -17,6 +17,9 @@ supportedWebViewEvents = [
'devtools-closed'
'devtools-focused'
'new-window'
'will-navigate'
'did-navigate-to-different-page'
'did-navigate-in-page'
'close'
'crashed'
'gpu-crashed'

Просмотреть файл

@ -19,6 +19,9 @@ WEB_VIEW_EVENTS =
'devtools-closed': []
'devtools-focused': []
'new-window': ['url', 'frameName', 'disposition', 'options']
'will-navigate': ['url']
'did-navigate-to-different-page': ['url']
'did-navigate-in-page': ['url']
'close': []
'crashed': []
'gpu-crashed': []

Просмотреть файл

@ -135,8 +135,22 @@ Emitted when a user or the page wants to start navigation. It can happen when th
This event will not emit when the navigation is started programmatically with
APIs like `webContents.loadURL` and `webContents.back`.
It is also not emitted during in-page navigation, such as clicking anchor links
or updating the `window.location.hash`. Use `did-navigate-in-page` for this purpose.
Calling `event.preventDefault()` will prevent the navigation.
### Event: 'did-navigate-to-different-page'
Emitted when the new page that was navigated to is different from the previous
page.
### Event: 'did-navigate-in-page'
Emitted when the page url changes but does not cause navigation outside of the page.
Examples of this occurring are when anchor links are clicked or when the
DOM `hashchange` event is triggered.
### Event: 'crashed'
Emitted when the renderer process has crashed.

Просмотреть файл

@ -576,6 +576,28 @@ webview.addEventListener('new-window', function(e) {
});
```
### Event: 'will-navigate'
Returns:
* `url` String
Emitted when a user or the page wants to start navigation. It can happen when the
`window.location` object is changed or a user clicks a link in the page. It not
emitted during in-page navigation such as clicking anchor links or updating the
`window.location.hash`. Use `did-navigate-in-page` for this purpose.
### Event: 'did-navigate-to-different-page'
Emitted when the new page that was navigated to is different from the previous
page.
### Event: 'did-navigate-in-page'
Emitted when the page url changes but does not cause navigation outside of the page.
Examples of this occurring are when anchor links are clicked or when the
DOM `hashchange` event is triggered.
### Event: 'close'
Fired when the guest page attempts to close itself.

Просмотреть файл

@ -0,0 +1,9 @@
<html>
<body>
<script type="text/javascript">
onload = function() {
window.location.hash = 'test';
}
</script>
</body>
</html>

Просмотреть файл

@ -0,0 +1,9 @@
<html>
<body>
<script type="text/javascript">
onload = function() {
window.history.replaceState('test', 'test', 'http://host/');
}
</script>
</body>
</html>

12
spec/fixtures/pages/webview-did-navigate-in-page.html поставляемый Normal file
Просмотреть файл

@ -0,0 +1,12 @@
<html>
<body>
<a href="#test_content" id="test_link">Click me.</a>
<span id="test_content">This is content.</span>
<script type="text/javascript">
onload = function() {
var a = document.getElementById('test_link');
a.click();
}
</script>
</body>
</html>

11
spec/fixtures/pages/webview-will-navigate.html поставляемый Normal file
Просмотреть файл

@ -0,0 +1,11 @@
<html>
<body>
<a id="test_link" href="http://host/">Test</a>
<script type="text/javascript">
onload = function() {
var a = document.getElementById('test_link');
a.click();
}
</script>
</body>
</html>

Просмотреть файл

@ -271,6 +271,53 @@ describe '<webview> tag', ->
webview.src = "file://#{fixtures}/pages/a.html"
document.body.appendChild webview
describe 'will-navigate event', ->
it 'emits when a url that leads to oustide of the page is clicked', (done) ->
webview.addEventListener 'will-navigate', (e) ->
assert.equal e.url, "http://host/"
done()
webview.src = "file://#{fixtures}/pages/webview-will-navigate.html"
document.body.appendChild webview
describe 'did-navigate-to-different-page event', ->
page_url = "file://#{fixtures}/pages/webview-will-navigate.html"
it 'emits when a url that leads to outside of the page is clicked', (done) ->
webview.addEventListener 'did-navigate-to-different-page', (e) ->
assert.equal e.url, page_url
done()
webview.src = page_url
document.body.appendChild webview
describe 'did-navigate-in-page event', ->
it 'emits when an anchor link is clicked', (done) ->
page_url = "file://#{fixtures}/pages/webview-did-navigate-in-page.html"
webview.addEventListener 'did-navigate-in-page', (e) ->
assert.equal e.url, "#{page_url}#test_content"
done()
webview.src = page_url
document.body.appendChild webview
it 'emits when window.history.replaceState is called', (done) ->
webview.addEventListener 'did-navigate-in-page', (e) ->
assert.equal e.url, "http://host/"
done()
webview.src = "file://#{fixtures}/pages/webview-did-navigate-in-page-with-history.html"
document.body.appendChild webview
it 'emits when window.location.hash is changed', (done) ->
page_url = "file://#{fixtures}/pages/webview-did-navigate-in-page-with-hash.html"
webview.addEventListener 'did-navigate-in-page', (e) ->
assert.equal e.url, "#{page_url}#test"
done()
webview.src = page_url
document.body.appendChild webview
describe 'close event', ->
it 'should fire when interior page calls window.close', (done) ->
webview.addEventListener 'close', ->