Giter Site home page Giter Site logo

chlodalejandro / parsoid-document Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 759 KB

A userscript library for easily handling Parsoid DOM trees.

Home Page: https://chlodalejandro.github.io/parsoid-document/

License: Other

JavaScript 8.02% TypeScript 90.61% Shell 1.36%
javascript mediawiki parsoid wiki wikimedia

parsoid-document's Introduction

ParsoidDocument

npm version npm downloads

Originally from User:Chlod/Scripts/ParsoidDocument on the English Wikipedia.

ParsoidDocument is an ES9+ library which implements a Parsoid-compatible document handler using an HTML5 IFrame. It is not a userscript, but is instead loaded by other userscripts. The IFrame contains the Parsoid document, which can then be modified using standard DOM functions. This is used to perform Parsoid-dependent operations in the browser without having to pull in the entirety of the VisualEditor codebase. The library works even without the existence of MediaWiki notification and Title modules, although they are helpful in ensuring data stability.

To be fully optimized, this should be implemented as a gadget and loaded through mw.loader. Note that some APIs used by this library may not be available on older browsers.

Installation

As a developer, insert the following code in the initialization section of your userscript. This is the only way to use the library on the English Wikipedia, and for most Wikimedia wikis.

// The "await" is optional, but ensures that the script has loaded and run before proceeding.
// On the English Wikipedia
await mw.loader.getScript("https://en.wikipedia.org/wiki/User:Chlod/Scripts/ParsoidDocument.js?action=raw&ctype=text/javascript");

// On other wikis, you must upload ParsoidDocument.js from the English Wikipedia or this repository
// first, and then change the URL to lead to the correct page. Make sure to keep the
// `?action=raw&ctype=text/javascript` at the end of the URL!

If it is available as a gadget, you can instead use the following.

mw.loader.load("ext.gadget.ParsoidDocument"); // where ParsoidDocument is the ID of the gadget.

If your userscript is bundled with Webpack, you can also install the @chlodalejandro/parsoid package. This package also adds typings for ParsoidDocument, in case you're developing with TypeScript or a decent IDE with a type checker.

npm install @chlodalejandro/parsoid

Usage

You can then access ParsoidDocument using the ParsoidDocument window global.

const parsoid = new ParsoidDocument();

parsoid.loadPage("User:Chlod/Scripts/ParsoidDocument");
parsoid.document.body.classList.contains("parsoid-body"); // true

// Prints the "data-mw" attribute of all transclusions.
parsoid.document.querySelectorAll("[typeof=\"mw:Transclusion\"]").forEach(v => {
    console.log(v.getAttribute("data-mw"));
});

// Convert the document, including any modification, to wikitext.
parsoid.toWikitext();

You can also extend the ParsoidDocument class as any other class.

class MyParsoidHandler extends ParsoidDocument {

    findAllTransclusions() {
        return this.document.querySelectorAll("[typeof=\"mw:Transclusion\"]");
    }

}

const parsoid = new MyParsoidHandler();
// ...
parsoid.findAllTransclusions();

See also

parsoid-document's People

Contributors

chlodalejandro avatar dependabot[bot] avatar

Watchers

 avatar

Forkers

saydough

parsoid-document's Issues

npm audit found vulnerabilities

# npm audit report

async  2.0.0 - 2.6.3
Severity: high
Prototype Pollution in async - https://github.com/advisories/GHSA-fwr7-v2mv-hh25
fix available via `npm audit fix`
node_modules/async

1 high severity vulnerability

To address all issues, run:
  npm audit fix

npm audit found vulnerabilities

# npm audit report

json5  2.0.0 - 2.2.1
Severity: high
Prototype Pollution in JSON5 via Parse Method - https://github.com/advisories/GHSA-9c47-m6qq-7p4h
fix available via `npm audit fix`
node_modules/json5

1 high severity vulnerability

To address all issues, run:
  npm audit fix

npm audit found vulnerabilities

# npm audit report

json5  2.0.0 - 2.2.1
Severity: high
Prototype Pollution in JSON5 via Parse Method - https://github.com/advisories/GHSA-9c47-m6qq-7p4h
fix available via `npm audit fix`
node_modules/json5

1 high severity vulnerability

To address all issues, run:
  npm audit fix

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.