-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata-link-reflector.user.js
287 lines (235 loc) · 9.07 KB
/
wikidata-link-reflector.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// ==UserScript==
// @name Wikidata Link Reflector
// @namespace https://www.wikidata.org/
// @version 0.1
// @description Suggest properties and values from links
// @author Lockal
// @license MIT
// @match https://www.wikidata.org/wiki/*
// @icon https://www.wikidata.org/static/favicon/wikidata.ico
// @grant unsafeWindow
// ==/UserScript==
/* eslint-disable unicorn/prefer-module */
/* eslint-env browser */
/* global unsafeWindow */
/* jshint esnext: false */
/* jshint esversion: 11 */
(function () {
'use strict';
// For release:
const reflectEndpoint = 'https://reflector.toolforge.org/api/v1/reflect';
// For debugging:
// const reflectEndpoint = 'http://localhost:5173/api/v1/reflect';
const attachUserscript = fn => {
const isUserscript = typeof unsafeWindow !== 'undefined';
const globalNamespace = isUserscript ? unsafeWindow : window;
const config = globalNamespace.RLCONF;
if (config === undefined) {
return;
}
const contentModel = config.wgPageContentModel;
const isProbablyEditable = config.wgIsProbablyEditable;
const isRedirect = config.wgIsRedirect;
const isEditView = config.wbIsEditView;
if (!['wikibase-item', 'wikibase-property', 'wikibase-lexeme'].includes(contentModel) || !isProbablyEditable || isRedirect || !isEditView) {
return;
}
// Install global variable to disallow multiple versions
if (globalNamespace.wgReflectorLoaded) {
console.log('Wikidata Link Reflector: not loaded, because other version was already loaded');
return;
}
globalNamespace.wgReflectorLoaded = true;
if (isUserscript) {
(new window.MutationObserver((_changes, observer) => {
if (unsafeWindow.jQuery?.ui && unsafeWindow.mw?.hook) {
fn(unsafeWindow.jQuery, unsafeWindow.mw);
observer.disconnect();
}
})).observe(document, {childList: true, subtree: true});
} else {
window.mw.loader.using(['jquery.ui', 'wikibase.api.RepoApi']).then(() => {
fn(window.jQuery, window.mw);
});
}
};
attachUserscript(($, mw) => {
const fillPropertyWithValue = (propInput, suggestion) => {
const entityselector = propInput.data('entityselector');
const snakValueNode = propInput.closest('.wikibase-snakview').data('snakview').$snakValue[0];
(new window.MutationObserver((changes, observer) => {
for (const change of changes) {
for (const addedNode of change.addedNodes) {
if (addedNode.classList.contains('valueview-input')) {
// Set the value and notify view about change
$(addedNode).val(suggestion.value).trigger('input');
observer.disconnect();
}
}
}
})).observe(snakValueNode, {childList: true, subtree: true});
const propertyLabel = suggestion.label || suggestion.id;
entityselector.element.val(propertyLabel);
entityselector._select(suggestion);
entityselector._close();
};
const createLabelFromSuggestion = (element, suggestion) => {
const $label = $('<span>', {class: 'ui-entityselector-label'}).text(suggestion.label || suggestion.id);
const $description = $('<span>', {class: 'ui-entityselector-description'});
if (suggestion.description && suggestion.description.length > 0) {
$description.text(suggestion.description);
}
const $linkBlock = $('<span>', {class: 'ui-entityselector-itemcontent'}).append($label).append($description);
const action = () => {
fillPropertyWithValue(element, suggestion);
};
const url = '/wiki/Property:' + suggestion.id;
const item = new $.ui.ooMenu.CustomItem($linkBlock, null, action, null, url);
item.source = 'lsuggester-tool';
return item;
};
const getContext = element => {
if (element.closest('.wikibase-statementview-qualifiers').length > 0) {
return 'qualifier';
}
if (element.closest('.wikibase-statementview-references').length > 0) {
return 'reference';
}
if (element.closest('.wikibase-statementview-mainsnak').length > 0) {
return 'mainsnak';
}
return 'other';
};
const getPropertyId = element => {
try {
return element.closest('.wikibase-statementlistview').find('.wikibase-statementview-mainsnak .wikibase-snakview.wb-edit').data('snakview')?.propertyId();
} catch {
// Pass
}
};
const getSubpropertyId = element => {
try {
return element.closest('.wikibase-snakview').data('snakview')?.propertyId();
} catch {
// Pass
}
};
const reflect = async options => {
const response = await fetch(reflectEndpoint + '?' + new URLSearchParams(options).toString());
const responseData = await response.json();
return responseData.items;
};
const canBeReflected = term => {
if (/^https?:\/\/(?:www\.|m\.|test\.)?wikidata\.org\//.test(term)) {
return false;
}
if (!/^https?:\/\//.test(term)) {
return false;
}
return true;
};
const onEntityselectorSearch = ({element, options, term}, addPromise) => {
if (['item', 'property'].includes(options.type) && !canBeReflected(term)) {
return;
}
// Assuming context === 'predicate' => type === 'property'
// eslint-disable-next-line no-unused-vars
const context = element.closest('.wikibase-snakview-property-container').length > 0 ? 'predicate' : 'value';
const subcontext = getContext(element);
const type = options.type;
const lang = options.language;
// eslint-disable-next-line no-unused-vars
const property = subcontext === 'qualifier' || subcontext === 'reference' ? getPropertyId(element) : undefined;
// Remove old entries, if any
options.menu.option('customItems', options.menu.option('customItems').filter(x => x.source !== 'lsuggester-tool'));
// Attach random value to input to track and prevent race conditions
const oracle = Math.random();
element.linkSuggesterLoadOracle = oracle;
const load = async () => {
let suggestions;
try {
suggestions = await reflect({url: term, lang, type});
} catch (error) {
console.error('Wikidata Link Reflector: Unable to call reflect API', error.message);
return [];
}
if (element.linkSuggesterLoadOracle !== oracle) {
// Reject obsolete request
return [];
}
if (suggestions.length > 0) {
const customItems = options.menu.option('customItems');
for (const option of customItems) {
if (option.getCssClass() === 'ui-entityselector-notfound') {
option.setVisibility(false);
break;
}
}
customItems.unshift(...suggestions.map(suggestion => createLabelFromSuggestion(element, suggestion)));
}
return [];
};
addPromise(load());
};
mw.hook('wikibase.entityselector.search').add(onEntityselectorSearch);
// Suggester for string inputs
$.widget('ui.wikidatalinkreflectorsuggester', $.ui.suggester, {
_create() {
$.ui.suggester.prototype._create.call(this);
this.options.menu.element.addClass('ui-entityselector-list');
},
destroy() {
$.ui.suggester.prototype.destroy.call(this);
},
_getSuggestions(term) {
// Hello darling my old jquery... Caller expects to see done(), so no promises here.
// eslint-disable-next-line new-cap
const deferred = $.Deferred();
if (!canBeReflected(term)) {
return deferred.resolve([], term).promise();
}
const property = this.options.subproperty ?? this.options.property;
const lang = this.options.language;
reflect({url: term, property, lang})
.then(items => deferred.resolve(items, term)).catch(error => deferred.reject(error));
return deferred.promise();
},
_createMenuItemFromSuggestion(suggestion) {
const $label = $('<span>', {class: 'ui-entityselector-label'}).text(suggestion.value);
const $description = $('<span>', {class: 'ui-entityselector-description'}).text('Automatically generated from link');
const $item = $('<span>', {class: 'ui-entityselector-itemcontent'}).append($label).append($description);
const value = suggestion.value;
return new $.ui.ooMenu.Item($item, value, null);
},
_initMenu(ooMenu) {
$.ui.suggester.prototype._initMenu.call(this, ooMenu);
$(this.options.menu)
.on('selected.suggester', () => {
// Notify the view about the change
this.element.trigger('eachchange');
// Clear menu, so it does not resuggest value on click
this.options.menu.option('items', []);
});
return ooMenu;
},
});
const createValueSuggester = ($input, options) => {
$input.wikidatalinkreflectorsuggester(options);
// Remove focus from the original text field
$input.off('blur');
};
$('.wikibase-statementgrouplistview', this).on('valueviewafterstartediting', event => {
const element = $(event.target);
const $input = element.find('.valueview-expert-StringValue-input');
if ($input.length === 0) {
return; // Not a StringValue statement
}
const subcontext = getContext(element);
const property = getPropertyId(element);
const subproperty = subcontext === 'qualifier' || subcontext === 'reference' ? getSubpropertyId(element) : undefined;
const language = mw.config.get('wgUserLanguage');
createValueSuggester($input, {subcontext, property, subproperty, language});
});
console.log('Wikidata Link Reflector: gadget loaded');
});
})();