GithubHelp home page GithubHelp logo

isabella232 / text-fragments-polyfill Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/text-fragments-polyfill

0.0 0.0 0.0 235 KB

License: Apache License 2.0

JavaScript 97.64% HTML 2.36%

text-fragments-polyfill's Introduction

Text Fragments Polyfill

This is a polyfill for the Text Fragments feature for browsers that don't support it directly.

The text fragment link #:~:text=This%20article%20is,kept%20as%20a%20pet rendered in Safari on an iOS device, with text-fragments.js injected via Web Inspector.

The implementation broadly follows the text-finding algorithms, with a few small deviations and reorganizations, and should perform similarly to renderers which have implemented this natively. It is used in Chromium for iOS as well as the Link to Text Fragment Browser Extension.

The src directory contains two files: text-fragments.js, which is the polyfilling mechanism, and text-fragment-utils.js, a module of util functions related to parsing, finding, and highlighting text fragments within the DOM.

The tools directory contains a util script used for generating a regex used in the utils module.

The test directory contains unit tests, based on Karma and Jasmine, as well as HTML files which can be loaded during those tests.

Installation

From npm:

npm install text-fragments-polyfill

From unpkg:

<script type="module>
  if (!('fragmentDirective' in Location.prototype) &&
      !('fragmentDirective' in document)) {
    import('https://unpkg.com/text-fragments-polyfill');
  }
</script>

Usage

For simple usage as a polyfill, it is sufficient to import the text-fragments.js file:

// Only load the polyfill in browsers that need it.
if (
  !('fragmentDirective' in Location.prototype) &&
  !('fragmentDirective' in document)
) {
  import('text-fragments.js');
}

Users who wish to take a more hands-on approach can reuse chunks of the logic by importing the text-fragment-utils.js module; support is provided for inclusion either as an ES6 module or using the Closure compiler.

Demo

Try the demo on a browser that does not support Text Fragments.

Development

  1. Hack in /src.
  2. Run npm run start and open http://localhost:8080/demo/ in a browser that does not support Text Fragments URLs directly, for example, Safari.
  3. Hack, reload, hack,โ€ฆ
  4. You can modify the Text Fragments URLs directly in /demo/index.html (look for location.hash).

License

Apache 2.0. This is not an official Google product.

text-fragments-polyfill's People

Contributors

jesusdelaprog avatar seblalancette avatar sebsg avatar tfmar avatar tomayac 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.