Giter Site home page Giter Site logo

reload-on-idle's Introduction

reload-on-idle's People

Contributors

fujimotos avatar piroor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reload-on-idle's Issues

アドオンのWebExtensions対応

WebExtensionsへの移行について

MozillaのAPIリファレンスを読んでいて気がついたのですが、
このアドオンは次の3つのAPIを使えばWebExtensions化できそうです:

大きな判定処理のフローとしては、

  1. 端末のアクティブ/休眠状態をIdle APIを通じて取得する
  2. 判定条件「X秒間活動無し」を満たしていれば、Tabs APIを通じて各タブにリロードをかける

という流れです。この判定処理を、Alarms APIを使って背後で定期的に立ち上げれば、
今のXPCOMベースのアドオンとほぼ同等の機能性を実現できるのでは、と考えています。

実装サンプル

言葉の説明だけだと分かりにくいので、上の処理の骨組だけを実際に組み立ててみました:

background.js

function reloadTabs() {
  browser.tabs.query({})
    .then((tabs) => {
      for (let tab of tabs) {
        browser.tabs.reload(tab.id);
      }
    });
};

/**
 * Check if the computer is in idle state
 */
browser.alarms.onAlarm.addListener((alarm) => {
  browser.idle.queryState(120)  // 2 min
    .then((info) => {
      if (info === 'idle') {
        reloadTabs();
      }
    });
});

/**
 * Create a periodic alarm
 */
browser.alarms.create("idle-reload-alarm", {
  periodInMinutes: 0.5 // 30 sec
});

アドオンの外装を作って、以上のコードをFirefox実機で動かしてみましたが、
2分間なにも操作をしないと、自動的にタブのリロードが走るところまで確認できました。

対応できる範囲/制約

どこまで細かい機能性を再現できるかですが、WebExtensionsのAPI仕様を読む限り、
次の3つのオプションは問題なく対応できそうです:

  • 「対象タブをURLでフィルタする」 -> tabs.Tab.urlで判定
  • 「読み込み中でもリロードする」 -> tabs.Tab.statusで判定
  • 「コンソールにログを出力する」 -> 可能

ただ、次の機能は対応するAPIが見つからなかったです:

  • 「編集中のデータがある場合も強制的にリロードする」(document.beforeunload対策?)

また、WebExtensionsのIdle APIだと休眠判定できる最小の時間間隔が「15秒」のようです。
実際に、browsers.idle.queryState(10) を実行すると次のエラーがでます:

2017-09-16-122814_418x43_scrot

(ただし、数秒間隔で自動リロードするニーズがあるかはよく分かっていません)


@piroor 以上の方針についてどう思われますか?
大きな見落としがなさそうなら、この方向性で少し取り組んでみたいと思います。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.