Giter Site home page Giter Site logo

tinywebex / randomtips Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 168 KB

Shows a random non obstrusive tip to the user under some constraints.

Home Page: https://tinywebex.github.io/RandomTips/

License: MIT License

JavaScript 98.08% CSS 0.38% HTML 1.54%
web-extension web-extensions browser-extension browser-addon firefox-addon firefox-extension

randomtips's Introduction

TinyWebEx RandomTips

Makes it possibly to easily specify tips to show to the user in a random, non-obstrusive way and allows you to set constraints for each tip. It is basically an extension of the MessageHandler module and automatically shows such a tip from a list of tips you specify.

Features

  • has an extensive and flexible list of contraints you can use to define in which cases a tip should (not) be shown (See "Specifying tips" below)
  • easy setup
  • low amount of work, after you
  • integrates into other TinyWebEx modules for showing messages and saving config values

HTML Setup

As the MessageHandler module requires the message HTML to be pre-defined, you have to define it for this module, too. In your .message-container you need to add this:

<div id="messageTips" aria-label="info message" class="message-box info invisible fade-hide">
  <span class="message-text"></span>
  <a href="#">
    <button type="button" class="message-action-button micro-button info invisible"></button>
  </a>
  <img class="icon-dismiss invisible" src="/common/img/close.svg" width="24" height="24" tabindex="0" data-i18n data-i18n-aria-label="__MSG_dismissIconDescription__"></span>
</div>

It's just the default message markup, it uses the "info" style here and an ID messageTips, so it can find the message and register it.

Using

A full example of how to use it, is the following:

// fetch tips from somewhere..

// init RandomTips
RandomTips.init(tips).then(() => {
    RandomTips.setContext("options");
    RandomTips.showRandomTipIfWanted();
});

This does the following steps:

  1. Initializes the module with the list of all tips (the tip data).
  2. When they are loaded, sets a specific context, which allows you to show some tips only in some "contexts".
  3. Shows a tip with a chance of 20%, so on average only in 1 of 5 triggers of this function, a tip is shown.

Instead of the last step, you can also use showRandomTip to force it to evaluate showing a tip. Note it still depends on your tip spec whether any (and if so which) tip is actually shown.

Specifying tips

Tips are specified as a list (array) of objects. Each object represents on tip and lists the constraints under which the tip may be shown or may not be shown. These are evaluated of this module, but only after the global constraint(s), i.e. showRandomTipIfWanted (see above), passed.

There are many constraints you can configure/set, so it would not be useful to list them here, but you can look them up in the JSDOC. Also, you can see the same in the example source code of a tips specification. You can copy this to your ../data dir (relative to this repo), but as the object is loaded via the init method, is it not required to be at any specific place.

Context

The .setContext method is somewhat special, as it is optional, but recommend to call before you actually (try to) show a tip. It basically just sets a string value to let you specify the context you have when you are calling RandomTips, so you can differentiate whether you are e.g. calling it from your add-on's popup, options page or some other place. You can thus easily limit where (i.e. in which "contexts") your tip may be shown or hidden.

Addon settings

This module needs to save some data in order to count the times the addon has been "opened" (i.e. the function has been triggered) or it needs to be saved how often a specific tip has been shown (in some context etc.).

For getting and saving the data, it uses the AddonSettings module. It does use the setting name randomTips and includes all data in potentially multiple nested objects there. Note that it itself does add another level of caching, i.e. it saves that object in it's module when it's init method is called.

In order to properly use this module, you have to set the following default options, i.e. add this to your DEFAULT_SETTINGS:

randomTips: {
    tips: {}
}

Tests

It is recommend that the tip objects is static and frozen. To be able to easily test this, a test spec for this is included in this repo. Note if you want to include it as it is, it only works if the tip data is saved in ../data/Tips.js (relative to this repo).

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.