https://github.com/wikimedia/wikipedia-preview
https://github.com/wikimedia/wikipedia-preview
wikipedia
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/wikimedia/wikipedia-preview
- Owner: wikimedia
- License: mit
- Created: 2019-12-04T20:17:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-24T12:26:54.000Z (3 months ago)
- Last Synced: 2025-03-30T18:08:37.914Z (3 months ago)
- Topics: wikipedia
- Language: JavaScript
- Homepage: https://wikimedia.github.io/wikipedia-preview/main
- Size: 44.4 MB
- Stars: 67
- Watchers: 8
- Forks: 22
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/wikimedia/wikipedia-preview/actions/workflows/main.yml)
# Wikipedia Preview
Wikipedia Preview is a JavaScript component that allows you to provide context from Wikipedia about words or phrases on any website. It lets you show a popup card with a short summary from Wikipedia when a reader hovers over a link.
Tasks and issues are tracked on [Phabricator](https://phabricator.wikimedia.org/project/profile/4393/).
Desktop | Mobile | Dark mode | Fullscreen gallery
--- | --- | --- | ---|
|
|
Storybook: https://wikimedia.github.io/wikipedia-preview/main/storybook
## Compatibility
Browsers | Versions
--- | ---
Chrome, Firefox, Opera, Edge | Current and previous version
Internet Explorer | 11+
Safari | 5.1+
iOS | 6.1+
Android | 4.1+## Features
* Works with any link that has an article on Wikipedia
* Supports LTR and RTL languages
* Works for articles with or without a lead image## Getting Started
Integrating Wikipedia Preview to your site consists of a three-step process:
1. Loading wikipedia-preview.js (as a standalone script, WordPress plugin, or an npm dependency)
2. Invoking `wikipediaPreview.init( )`
3. Annotating articles accordinglyYou can read more about each step below. Once Wikipedia Preview is set up correctly, you should see the version information being logged in your JS console. You can also invoke `wikipediaPreview.version()` from your JS console to view version information at any time.
### WordPress Plugin
A WordPress plugin providing a thin wrapper around Wikipedia Preview to simplify its integration and usage within WordPress is available on [GitHub](https://github.com/wikimedia/wikipediapreview-wordpress) and [WordPress.org](http://wordpress.org/plugins/wikipedia-preview/).
### Standalone Script
```html
wikipediaPreview.init()
```
You can serve the file yourself or include it from [unpkg](https://unpkg.com/browse/wikipedia-preview@latest/dist/wikipedia-preview.umd.cjs) (The library version 1.9.0 and later cannot be loaded from Unpkg due to a [MIME type mismatch issue](https://github.com/mjackson/unpkg/issues/355), the current [workaround solution](https://github.com/mjackson/unpkg/issues/355#issuecomment-1541524887) is adding the `?module` parameter to get the correct content-type).### NPM
```bash
$ npm install wikipedia-preview --save
``````javascript
const wikipediaPreview = require('wikipedia-preview')
wikipediaPreview.init()
```### Options of the init function
The `init` function accepts the following options:
Name | Type | Default | Description
--- | --- | --- | ---
root | string,Element,Element[] | `document` | Where to look for elements that should have the popup selector. Can be a selector to locate the root, a DOM Element, or an array of Elements
selector | string | `'[data-wikipedia-preview]'` | How nodes that should have the popup are identified
lang | string | `'en'` | Default Wikipedia language
popupContainer | string,Element | `document.body` | Where to put the popup in the DOM. Can be a selector or a DOM element.
detectLinks | Boolean | `false` | Allow Wikipedia hyperlinks to have the popup
events | Object | `{}` | Custom event handlers: `{ onShow: , onWikiRead: }`
debug | Boolean | `false` | Shows the debug message when `init()` is called
prefersColorScheme | string | `'detect'` | Sets theme color. Allowed values are 'light', 'dark' and 'detect'. Setting it to 'light' or 'dark' will dictate theme color regardless of [`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme); setting to 'detect' will render preview according to [`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme).Example (custom selector)
```html
You can learn about Chat and Chien from Wikipedia.
``````javascript
wikipediaPreview.init({
root: document.querySelector('.content'),
selector: '.wiki',
popupContainer: '.popup-container',
lang: 'fr'
});
```Example (detect Wikipedia links)
```html
You can learn about Chat and Chien from Wikipedia.
``````javascript
wikipediaPreview.init({
detectLinks: true
});
```Example (custom event handlers)
```javascript
wikipediaPreview.init({
events: {
onShow: function(title, lang, type) {
// call custom instrumentation here
},
onWikiRead: function(title, lang) {
// call custom instrumentation here
}
}
});
```### Attributes
#### data-wikipedia-preview
To indicate that a word or expression should bring up the article preview popup, mark it with the `data-wikipedia-preview` attribute (or anything else as long as you're using the `selector` option described above).
#### data-wp-title
When the article title is not the same as the node's `textContent` property, use the `data-wp-title` attribute to specify the article title.
Note that the article title can include a section id. For example: `Europe#Classical_antiquity`
#### data-wp-lang
To use a language different than the language specified in the options, use the `data-wp-lang` attribute.
#### .wmf-wp-with-preview
To use the default trigger link styling:
* Add the following link to the page header:
```html```
* Add the class `wmf-wp-with-preview` to the nodeIf you prefer to style them in a way that makes more sense for your context, simply don't include the `wikipedia-preview-link.css` link in the header and add your own class to the node. Both of these are valid ways:
```CSS
[data-wikipedia-preview] {
background-color: yellow;
}
``````CSS
.my-own-css-style {
background-color: yellow;
}
```#### CSS custom properties
If you wish to adjust the styling of the light/dark theme, you can override the following CSS custom properties to your liking as shown below, under the appropriate [`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) query.
```CSS
@media (prefers-color-scheme: dark) {
.wikipediapreview {
--wikipediapreview-primary-background-color: #202122;
--wikipediapreview-secondary-background-color: #202122;
--wikipediapreview-primary-color: #eaecf0;
--wikipediapreview-filter-setting: invert(1);
}
}
```## Acknowledgements/Contributors
This is heavily inspired by [jquery.wikilookup](https://github.com/mooeypoo/jquery.wikilookup) and [Page Previews](https://www.mediawiki.org/wiki/Page_Previews).