tabs.toggleReaderMode() 编辑

Toggles Reader Mode for the given tab.

    This function toggles Reader Mode for the given tab. It takes a tab ID as a parameter: if this is omitted, the currently active tab is toggled.

    This is an asynchronous function that returns a Promise.

    Reader Mode, also known as Reader View, is a browser feature that makes it easier for the user to focus on an article by:

    • hiding non-essential page elements like sidebars, footers, and ads
    • changing the page's text size, contrast and layout for better readability.

    Reader Mode is useful specifically for articles: meaning, pages that have a body of text content as their main feature. Pages that don't have an identifiable article are not eligible for display in Reader Mode. To find out whether a page is an article, check the isArticle property of tabs.Tab.

    To find out whether a tab is already in Reader Mode, check the isInReaderMode property of tabs.Tab. To track tabs changing into or out of Reader Mode, you'll need to keep track of the current state of all tabs, and check when isInReaderMode changes:

    function handleUpdated(tabId, changeInfo, tabInfo) {
      if (changeInfo.status === "complete") {
        console.log(`Tab ${tabId} reader mode: ${tabInfo.isInReaderMode}`);
      }
    }
    
    browser.tabs.onUpdated.addListener(handleUpdated);

    Syntax

    var toggling = browser.tabs.toggleReaderMode(
      tabId            // optional integer
    )
    

    Parameters

    tabIdOptional
    integer. The ID of the tab to display in Reader Mode. Defaults to the selected tab of the current window.

    Return value

    A Promise that will be fulfilled with no arguments when the tab has been updated. If any error occurs (for example, because the page was not an article), the promise will be rejected with an error message.

    Examples

    This code switches every new page into Reader Mode, if that page is eligible for it:

    function switchToReaderMode(tabId, changeInfo, tabInfo) {
      if (changeInfo.isArticle) {
        browser.tabs.toggleReaderMode(tabId);
      }
    }
    
    browser.tabs.onUpdated.addListener(switchToReaderMode);

    Browser compatibility

    BCD tables only load in the browser

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
    列表为空,暂无数据

    词条统计

    浏览:108 次

    字数:3540

    最后编辑:6 年前

    编辑次数:0 次

      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文