webview: will-navigate and page-favicon-set navigation events
This commit is contained in:
parent
c424d0e9f3
commit
9b585458c1
9 changed files with 72 additions and 3 deletions
|
@ -256,6 +256,19 @@ void WebContents::TitleWasSet(content::NavigationEntry* entry,
|
|||
Emit("page-title-set", entry->GetTitle(), explicit_set);
|
||||
}
|
||||
|
||||
void WebContents::DidUpdateFaviconURL(
|
||||
const std::vector<content::FaviconURL>& urls) {
|
||||
std::set<GURL> unique_urls;
|
||||
for (auto iter = urls.begin(); iter != urls.end(); ++iter) {
|
||||
if (iter->icon_type != content::FaviconURL::FAVICON)
|
||||
continue;
|
||||
const GURL& url = iter->icon_url;
|
||||
if (url.is_valid())
|
||||
unique_urls.insert(url);
|
||||
}
|
||||
Emit("page-favicon-set", unique_urls);
|
||||
}
|
||||
|
||||
bool WebContents::OnMessageReceived(const IPC::Message& message) {
|
||||
bool handled = true;
|
||||
IPC_BEGIN_MESSAGE_MAP(WebContents, message)
|
||||
|
|
|
@ -6,10 +6,13 @@
|
|||
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "brightray/browser/default_web_contents_delegate.h"
|
||||
#include "content/public/browser/browser_plugin_guest_delegate.h"
|
||||
#include "content/public/common/favicon_url.h"
|
||||
#include "content/public/browser/web_contents_delegate.h"
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
@ -175,6 +178,8 @@ class WebContents : public mate::EventEmitter,
|
|||
void NavigationEntryCommitted(
|
||||
const content::LoadCommittedDetails& load_details) override;
|
||||
void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override;
|
||||
void DidUpdateFaviconURL(
|
||||
const std::vector<content::FaviconURL>& urls) override;
|
||||
|
||||
// content::BrowserPluginGuestDelegate:
|
||||
void DidAttach(int guest_proxy_routing_id) final;
|
||||
|
|
|
@ -16,6 +16,7 @@ supportedWebViewEvents = [
|
|||
'crashed'
|
||||
'destroyed'
|
||||
'page-title-set'
|
||||
'page-favicon-set'
|
||||
]
|
||||
|
||||
nextInstanceId = 0
|
||||
|
|
|
@ -18,6 +18,7 @@ WEB_VIEW_EVENTS =
|
|||
'crashed': []
|
||||
'destroyed': []
|
||||
'page-title-set': ['title', 'explicitSet']
|
||||
'page-favicon-set': ['favicons']
|
||||
|
||||
dispatchEvent = (webView, event, args...) ->
|
||||
throw new Error("Unkown event #{event}") unless WEB_VIEW_EVENTS[event]?
|
||||
|
|
|
@ -154,6 +154,14 @@ class SrcAttribute extends WebViewAttribute
|
|||
not @.getValue()
|
||||
return
|
||||
|
||||
domEvent = new Event('will-navigate')
|
||||
domEvent['url'] = @getValue()
|
||||
domEvent.cancelable = true
|
||||
self = @
|
||||
domEvent.preventDefault = () ->
|
||||
self.setValueIgnoreMutation ''
|
||||
@webViewImpl.webviewNode.dispatchEvent domEvent
|
||||
|
||||
unless @webViewImpl.guestInstanceId?
|
||||
if @webViewImpl.beforeFirstNavigation
|
||||
@webViewImpl.beforeFirstNavigation = false
|
||||
|
@ -161,6 +169,7 @@ class SrcAttribute extends WebViewAttribute
|
|||
return
|
||||
|
||||
# Navigate to |this.src|.
|
||||
if @getValue()
|
||||
httpreferrer = @webViewImpl.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER].getValue()
|
||||
urlOptions = if httpreferrer then {httpreferrer} else {}
|
||||
remote.getGuestWebContents(@webViewImpl.guestInstanceId).loadUrl @getValue(), urlOptions
|
||||
|
|
|
@ -662,6 +662,13 @@ Emitted when details regarding a requested resource is available.
|
|||
|
||||
Emitted when a redirect was received while requesting a resource.
|
||||
|
||||
### Event: 'page-favicon-set'
|
||||
|
||||
* `event` Event
|
||||
* `favicons` [String]
|
||||
|
||||
Emitted when page receives favicon urls.
|
||||
|
||||
### Event: 'new-window'
|
||||
|
||||
* `event` Event
|
||||
|
|
|
@ -275,6 +275,13 @@ examples.
|
|||
|
||||
## DOM events
|
||||
|
||||
### will-navigate
|
||||
|
||||
* `url` String
|
||||
|
||||
Fired when view is about to navigate , calling `event.preventDefault()` will
|
||||
cancel the navigation.
|
||||
|
||||
### did-finish-load
|
||||
|
||||
Fired when the navigation is done, i.e. the spinner of the tab will stop
|
||||
|
@ -330,6 +337,12 @@ Fired when a redirect was received while requesting a resource.
|
|||
Fired when page title is set during navigation. `explicitSet` is false when title is synthesised from file
|
||||
url.
|
||||
|
||||
### page-favicon-set
|
||||
|
||||
* `favicons` [String]
|
||||
|
||||
Fired when page receives favicon urls.
|
||||
|
||||
### console-message
|
||||
|
||||
* `level` Integer
|
||||
|
|
2
spec/fixtures/pages/a.html
vendored
2
spec/fixtures/pages/a.html
vendored
|
@ -1,4 +1,6 @@
|
|||
<html>
|
||||
<link rel="icon" type="image/png" href="/favicon.png"/>
|
||||
<link rel="icon" type="image/png" href="http://test.com/favicon.png"/>
|
||||
<body>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
console.log('a');
|
||||
|
|
|
@ -151,3 +151,21 @@ describe '<webview> tag', ->
|
|||
done()
|
||||
webview.src = "file://#{fixtures}/pages/a.html"
|
||||
document.body.appendChild webview
|
||||
|
||||
describe 'will-navigate event', ->
|
||||
it 'is emitted before navigation', (done) ->
|
||||
webview.addEventListener 'will-navigate', (e) ->
|
||||
e.preventDefault()
|
||||
assert.equal webview.src, ''
|
||||
done()
|
||||
webview.src = "file://#{fixtures}/pages/a.html"
|
||||
document.body.appendChild webview
|
||||
|
||||
describe 'page-favicon-set event', ->
|
||||
it 'emits when favicon is set', (done) ->
|
||||
webview.addEventListener 'page-favicon-set', (e) ->
|
||||
assert.equal e.favicons.length, 2
|
||||
assert.equal e.favicons[0], 'file:///favicon.png'
|
||||
done()
|
||||
webview.src = "file://#{fixtures}/pages/a.html"
|
||||
document.body.appendChild webview
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue