如何仅在用户更改URL时触发事件? [英] How to trigger an event only when the user changes the URL?

查看:243
本文介绍了如何仅在用户更改URL时触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Chrome扩展程序,我想检测用户何时输入了网址。我知道:

  chrome.tabs.onUpdated.addListener(eventLisenerObj.onUpdated); 

但是,只要网址发生变化就会调用它(例如,当页面自动重新加载或用户点击一个链接等)



我希望能够确定URL只是由用户输入一个URL改变。

解决方案

您可以使用 webNavigation.onCommitted MDN 事件。事件侦听器收到一个属性 transitionType MDN ,这将是不同的值 MDN 。您触发的值取决于 准确您的要求。对于您所描述的内容,您可能需要 'typed' MDN ,但也可能 'generated' MDN 'keyword' MDN 和/或 'keyword_generated' < sup> MDN



Chrome的历史API页面(它们在Chrome上列出 webNavigation 页面,但这里没有解释)(在MDN上: TransitionType ) (来自Chrome历史记录API页面的文本):

lockquote


  • link

    用户点击另一页面上的链接进入该页面。

  • typed

    用户通过在地址栏中输入网址来获得此页面。也用于其他明确的导航操作。另请参阅 生成的 MDN ,用于用户选择看起来不像URL的选择。

  • auto_bookmark

    用户通过UI中的建议进入该页面 - 例如,通过菜单项。

  • auto_subframe

    子框架导航。这是自动加载到非顶级框架中的任何内容。例如,如果某个网页由多个包含广告的框架组成,则这些广告网址具有此转换类型。用户可能甚至没有意识到这些页面中的内容是一个单独的框架,因此可能不关心URL(另见 manual_subframe MDN )。 b $ b
  • manual_subframe

    对于用户明确要求的子帧导航并在后退/前进列表中生成新的导航条目。明确请求的帧可能比自动加载的帧更重要,因为用户可能关心所请求的帧已加载的事实。
  • generated
  • code>

    用户通过在地址栏中输入并选择一个看起来不像URL的条目来访问此页面。例如,匹配可能包含Google搜索结果页的网址,但用户可能会将其视为搜索Google for ...。这些与 键入 MDN 导航,因为用户没有输入或看到目标网址。另请参阅 关键字 MDN
  • auto_toplevel

    页面在命令行中指定或者是开始页面。
  • form_submit

    用户填写表格中的值并提交。请注意,在某些情况下(例如表单使用脚本提交内容时),提交表单不会导致此过渡类型。
  • code>

    用户通过单击重新加载按钮或在地址栏中按Enter来重新加载页面。会话恢复和重新打开关闭选项卡也使用此转换类型。
  • keyword

    URL是从默认搜索提供商以外的可替换关键字生成。另请参阅 keyword_generated MDN

  • keyword_generated

    对应于为关键字生成的访问。另请参阅 关键字 MDN


为了区分某些类型的转换,除了 transitionType 值,您还需要查看 TransitionQualifier MDN 。可能的值(来自 Chrome文档,它们的描述有些不同关于MDN ):



  • client_redirect

    一个或多个重定向通过JavaScript或页面上的元刷新标记在导航期间发生。

  • server_redirect

    One or or在导航过程中,服务器发送的HTTP标头会导致更多重定向。
  • 用户使用前进或后退按钮启动导航。
  • from_address_bar

    用户从地址栏(又名Omnibox)发起导航。



I am working on a Chrome extension, I want to detect when the user has typed a URL. I know about:

chrome.tabs.onUpdated.addListener(eventLisenerObj.onUpdated);

But, it gets called whenever the URL is changed (e.g. when the page is auto reloads, or user clicks on a link, etc.)

I desire to be able to determine that the URL was changed only by the user typing a URL.

解决方案

You can get this information using the webNavigation.onCommitted(MDN) event. The event listener receives a property transitionType(MDN), which will be different values(MDN) based on the cause of the navigation. Which values you trigger on will depend on exactly what you are desiring. For what you describe, you will probably want 'typed'(MDN), but potentially also 'generated'(MDN), 'keyword'(MDN) and/or 'keyword_generated'(MDN).

The list of possible values is explained on Chrome's History API page (they are listed on the Chrome webNavigation page, but not explained there) (On MDN: TransitionType) (text from the Chrome History API page):

  • "link"
    The user got to this page by clicking a link on another page.
  • "typed"
    The user got this page by typing the URL in the address bar. Also used for other explicit navigation actions. See also generated(MDN), which is used for cases where the user selected a choice that didn't look at all like a URL.
  • "auto_bookmark"
    The user got to this page through a suggestion in the UI — for example, through a menu item.
  • "auto_subframe"
    Subframe navigation. This is any content that is automatically loaded in a non-top-level frame. For example, if a page consists of several frames containing ads, those ad URLs have this transition type. The user may not even realize the content in these pages is a separate frame, and so may not care about the URL (see also manual_subframe(MDN)).
  • "manual_subframe"
    For subframe navigations that are explicitly requested by the user and generate new navigation entries in the back/forward list. An explicitly requested frame is probably more important than an automatically loaded frame because the user probably cares about the fact that the requested frame was loaded.
  • "generated"
    The user got to this page by typing in the address bar and selecting an entry that did not look like a URL. For example, a match might have the URL of a Google search result page, but it might appear to the user as "Search Google for ...". These are not quite the same as typed(MDN) navigations because the user didn't type or see the destination URL. See also keyword(MDN).
  • "auto_toplevel"
    The page was specified in the command line or is the start page.
  • "form_submit"
    The user filled out values in a form and submitted it. Note that in some situations — such as when a form uses script to submit contents — submitting a form does not result in this transition type.
  • "reload"
    The user reloaded the page, either by clicking the reload button or by pressing Enter in the address bar. Session restore and Reopen closed tab use this transition type, too.
  • "keyword"
    The URL was generated from a replaceable keyword other than the default search provider. See also keyword_generated(MDN).
  • "keyword_generated"
    Corresponds to a visit generated for a keyword. See also keyword(MDN).

To differentiate some types of transitions, in addition to the transitionType values, you will also want to look at the TransitionQualifier(MDN). The possible values are (from the Chrome documentation, which are described somewhat differently on MDN):

  • "client_redirect"
    One or more redirects caused by JavaScript or meta refresh tags on the page happened during the navigation.
  • "server_redirect"
    One or more redirects caused by HTTP headers sent from the server happened during the navigation.
  • "forward_back"
    The user used the Forward or Back button to initiate the navigation.
  • "from_address_bar"
    The user initiated the navigation from the address bar (aka Omnibox).

这篇关于如何仅在用户更改URL时触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆