GithubHelp home page GithubHelp logo

tweakjs's Introduction

tweak.js

This simple javascript library allows you to tweak the DOM of your pages simply by passing it an array of tweaks.

Use-cases:

tweak.js can be used to quickly implement:

  • A/B Testing
  • Text corrections
  • Replace images, styles, etc

Anybody can change tweaks:

  • Without changes to the underlying application
  • Without developer support
  • Without delay

Installation

npm install @linkorb/tweakjs

Demo

Check the demo/ directory for an demonstration + example on how to use tweak.js

How to use

To support tweak.js in your application, you'll need to include two tags in the <head /> of your application:

<script type="text/javascript" src="../tweak.js"></script>

tweak.js is responsible for parsing and executing the tweaks. Tweaks are JavaScript objects which describes how to tweak. Here is an example of an array of tweaks:

var tweaks = [
    {
        "description": "Set custom header text",
        "action": "setInnerText",
        "selector": "h1:first-of-type",
        "value": "This is a my custom header text",
    },
    {
        "description": "Change background color",
        "action": "setStyleAttribute",
        "selector": "body",
        "attribute": "background-color",
        "value": "#c0d0e0",
    }
];

The tweaks shall be loaded separately.

Loading tweaks

There are 3 ways to load tweaks

  1. Load tweaks from global variable - It should simply define a variable called tweaks that contains an array of tweak instructions. This array should be loaded before tweak.js
<script type="text/javascript" src="https://my.host/path/to/my/tweaks.js"></script>
<script type="text/javascript" src="../tweak.js"></script>
  1. Load tweaks in the document - A variable tweakjs is exposed to the global scope. Therefore, after the tweak.js gets loaded, you can still add tweaks like this in your HTML:
<script>
tweakjs.loader.load(
  [
    {
        "description": "Set div text to bold",
        "action": "setInnerText",
        "selector": ".post-load>div",
        "value": "This text is replaced after pageload.",
    },
    {
        "description": "Set custom inner HTML",
        "action": "setInnerHtml",
        "selector": ".post-load>span",
        "value": "<i>Italic</i>",
    }
  ]
)
</script>
  1. Load tweaks from external resource - Add a URL query parameter tweaksUrl to specify an external js file.
http://localhost:8080/demo?tweaksUrl=https://my.host/path/to/my/tweaks.js

Supported actions

  • setInnerText
  • setInnerHtml
  • setAttribute
  • setStyleAttribute
  • addClass
  • hide
  • show
  • removeClass
  • remove
  • addStyle
  • addScript
  • execute

You can find demo's of all the actions in the demo/ directory.

Build from source

To change the source file and build, simply use webpack.

# generates dist/tweak.js
webpack -d
# generates dist/tweak.min.js
webpack -p

Advanced usage

  • In A/B testing scenarios, you could have a server generate tweaks.js based on visitor properties
  • You could have your app generate the tweaks variable server-side and include it in the script tag's body (instead of the src attribute)

License

MIT (see LICENSE)

Brought to you by the LinkORB Engineering team


Check out our other projects at linkorb.com/engineering.

Btw, we're hiring!

tweakjs's People

Contributors

h-wang avatar joostfaassen avatar

Watchers

 avatar  avatar

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.