Skip to content

Commit

Permalink
Start on a web extension
Browse files Browse the repository at this point in the history
  • Loading branch information
arlolra committed Dec 16, 2016
1 parent d5b67ca commit 600382d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,2 +1,5 @@
suggestor
config.toml

webext/node_modules
webext/web-ext-artifacts
1 change: 1 addition & 0 deletions webext/.eslintignore
@@ -0,0 +1 @@
node_modules/*
7 changes: 7 additions & 0 deletions webext/.eslintrc
@@ -0,0 +1,7 @@
{
"extends": "eslint:recommended",
"env": {
"es6": true,
"browser": true
}
}
4 changes: 4 additions & 0 deletions webext/.jshintrc
@@ -0,0 +1,4 @@
{
"esversion": 6,
"browser": true
}
57 changes: 57 additions & 0 deletions webext/index.js
@@ -0,0 +1,57 @@
/* global $,mw,ve,OO */
let inject = function() {
const SUGGESTOR_URI = "https://tools.wmflabs.org/suggestor/post";

let post = function(summary, wikitext) {
let target = ve.init.target;

// FIXME: There's gotta be some canonical way of doing this.
let api = $("link[rel=EditURI]").attr("href");
api = api.replace(/\?.*$/, "");
if (/^\/\//.test(api)) {
api = location.protocol + api;
}

$.ajax({
type: "post",
url: SUGGESTOR_URI,
dataType: "json",
data: JSON.stringify({
api: api,
wikitext: wikitext,
summary: summary,
revid: mw.config.get("wgRevisionId"),
pageid: mw.config.get("wgArticleId"),
pagename: mw.config.get("wgPageName"),
}),
success: function() {
target.saveDialog.reset();
mw.hook("postEdit").fire({ message: "Edit suggested!" });
target.deactivate(true);
},
error: function() {
target.showSaveError("Failed to suggest edit.", true, true);
},
});
};

mw.libs.ve.targetLoader.addPlugin(function() {
let torget = function(config) {
torget.super.call(this, config);
};
OO.inheritClass(torget, ve.init.mw.DesktopArticleTarget);
torget.prototype.save = function(doc, options) {
ve.init.target.serialize(doc, post.bind(null, options.summary));
};
ve.init.mw.targetFactory.register(torget);
});

mw.hook("postEdit").fire({ message: "Suggestor loaded." });
};

// `content_scripts` can't see variables defined by page scripts, but we want
// to use the mw apis, so let's inject the script in the page.
let script = document.createElement("script");
script.setAttribute("id", "suggestor");
script.appendChild(document.createTextNode(inject.toSource() + "();"));
document.body.appendChild(script);
18 changes: 18 additions & 0 deletions webext/manifest.json
@@ -0,0 +1,18 @@
{
"manifest_version": 2,
"name": "Suggestor",
"version": "0.0.0",
"description": "Suggest edits.",
"applications": {
"gecko": {
"id": "suggestor@wmflabs",
"strict_min_version": "45.0"
}
},
"content_scripts": [
{
"matches": ["*://*.wikipedia.org/wiki/*"],
"js": [ "index.js" ]
}
]
}
19 changes: 19 additions & 0 deletions webext/package.json
@@ -0,0 +1,19 @@
{
"name": "suggestor",
"version": "0.0.0",
"description": "Suggest edits.",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"eslint": "^3.12.2",
"jshint": "^2.9.4",
"web-ext": "^1.6.0"
},
"scripts": {
"build": "web-ext build",
"eslint": "eslint --ext .js .",
"jshint": "jshint *.js",
"lint": "npm run wlint && npm run jshint && npm run eslint",
"wlint": "web-ext lint"
}
}

0 comments on commit 600382d

Please sign in to comment.