-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathao3-citation-generator.user.js
201 lines (175 loc) · 7.31 KB
/
ao3-citation-generator.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
// ==UserScript==
// @name AO3 Citation Generator
// @namespace https://github.com/redsummernight
// @version 1.0.4
// @description Cite fan works from AO3
// @author redsummernight
// @match *://*.archiveofourown.org/works/*
// @match *://*.archiveofourown.org/chapters/*
// @match *://*.archiveofourown.org/collections/*/works/*
// @grant GM_addStyle
// @downloadURL https://github.com/redsummernight/ao3-citation-generator/raw/main/ao3-citation-generator.user.js
// @supportURL https://github.com/redsummernight/ao3-citation-generator/issues
// @website https://github.com/redsummernight/ao3-citation-generator
// @homepageURL https://github.com/redsummernight/ao3-citation-generator
// ==/UserScript==
/* jshint newcap:false */
/* global console:false, GM_addStyle:false, module:false */
var ACG = (function() {
'use strict';
var m = {};
function getMonthName(index) {
var monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
return monthNames[index];
}
m.getWorkURL = function(url) {
var re = /(.*\/works\/\d+)*/;
url = url.split('#')[0];
url = re.exec(url)[1];
return url.replace(/\/collections\/\w+(?=\/)/, '');
};
m.getFandomDescription = function(fandoms) {
if (fandoms.length === 0) {
throw 'No fandom';
} else if (fandoms.length === 1) {
return fandoms[0];
} else if (fandoms.length < 4) {
return fandoms.join('/') + ' crossover';
} else {
return 'Multifandom';
}
};
m.getRelationshipDescription = function(relationship, category) {
var r = relationship;
if (category === 'M/M' || category === 'F/F') {
r += ' slash';
}
return r;
};
m.getQuotedTitle = function(title) {
var t = title.replace(/"/g, '\'');
var titleWithoutQuotes = t.replace(/["']/g, ''),
lastCharWithoutQuotes = titleWithoutQuotes.charAt(titleWithoutQuotes.length - 1);
if (['.', '?', '!'].indexOf(lastCharWithoutQuotes) === -1) {
t += '.';
}
return '"' + t + '"';
};
m.getCitation = function(info, style) {
if (style !== 'social-sciences' && style !== 'humanities') {
throw 'Unknown citation style';
}
var c = '';
c += info.author + '. ';
if (style === 'social-sciences') {
c += info.publishedDate.getUTCFullYear() + '. ';
}
c += m.getQuotedTitle(info.title) + ' ';
c += m.getFandomDescription(info.fandoms) + ' fan fiction. ';
if (info.relationships.length === 1 && info.categories.length === 1) {
c += m.getRelationshipDescription(info.relationships[0], info.categories[0]) + '. ';
}
c += '<em>Archive of Our Own</em>, ';
c += info.publishedDate.getUTCDate() + ' ' + getMonthName(info.publishedDate.getUTCMonth());
if (style === 'social-sciences') {
c += '. ';
} else if (style === 'humanities') {
c += ' ' + info.publishedDate.getUTCFullYear() + '. ';
}
c += '<a href="' + info.url + '">' + info.url + '</a>.';
return c;
};
return m;
})();
if (typeof module !== 'undefined' && module.hasOwnProperty('exports')) {
// Export Node module for testing
module.exports = ACG;
}
if (typeof document !== "undefined") {
// In browser: start user script
(function() {
'use strict';
var noSelectStyleClass = 'citation-noselect';
function getText(selector) {
return document.querySelector(selector).textContent.trim();
}
function getTextList(selector) {
var r = [],
elements = document.querySelectorAll(selector);
for (var i = 0; i < elements.length; i++) {
r.push(elements[i].textContent.trim());
}
return r;
}
function getUnselectableHTML(text) {
return '<span class="' + noSelectStyleClass + '">' + text + '</span>';
}
function getInfo() {
var url = document.URL;
var entireWork = document.querySelector('.chapter.entire a');
if (entireWork !== null) {
url = 'http://archiveofourown.org' + entireWork.getAttribute('href');
}
return {
url: ACG.getWorkURL(url),
title: getText('.title.heading'),
author: getText('[rel=author]'),
// Where the string is ISO 8601 date only, the UTC time zone is used to interpret arguments.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
publishedDate: new Date(getText('dd.published')),
categories: getTextList('dd.category.tags a.tag'),
fandoms: getTextList('dd.fandom.tags a.tag'),
relationships: getTextList('dd.relationship.tags a.tag'),
};
}
function insertCitation() {
if (document.getElementById('citation')) {
return;
}
var meta = document.querySelector('dl.work.meta');
var info = getInfo();
var dt = document.createElement('dt');
dt.id = 'citation';
dt.innerHTML = '<a href="http://www.transformativeworks.org/how-to-cite-fan-works/">Citation</a>: ';
meta.appendChild(dt);
var dd = document.createElement('dd');
var citationHTML = '<p>';
citationHTML += getUnselectableHTML('Social sciences style: ');
citationHTML += ACG.getCitation(info, 'social-sciences');
citationHTML += '</p><p>';
citationHTML += getUnselectableHTML('Humanities style: ');
citationHTML += ACG.getCitation(info, 'humanities');
citationHTML += '<p>';
dd.innerHTML = citationHTML;
meta.appendChild(dd);
}
insertCitation();
// Polyfill for Greasemonkey 4
// https://github.com/greasemonkey/gm4-polyfill/blob/a834d46afcc7d6f6297829876423f58bb14a0d97/gm4-polyfill.js#L32
if (typeof GM_addStyle == 'undefined') {
window.GM_addStyle = function(aCss) {
var head = document.getElementsByTagName('head')[0];
if (head) {
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = aCss;
head.appendChild(style);
return style;
}
return null;
};
}
// http://stackoverflow.com/a/4407335
GM_addStyle('.' + noSelectStyleClass + ' {' +
'-webkit-touch-callout: none; /* iOS Safari */' +
'-webkit-user-select: none; /* Chrome/Safari/Opera */' +
'-khtml-user-select: none; /* Konqueror */' +
'-moz-user-select: none; /* Firefox */' +
'-ms-user-select: none; /* Internet Explorer/Edge */' +
'user-select: none; /* Non-prefixed version, currently not supported by any browser */'
);
})();
}