-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexolve-embedder.js
More file actions
137 lines (125 loc) · 3.7 KB
/
exolve-embedder.js
File metadata and controls
137 lines (125 loc) · 3.7 KB
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
/*
MIT License
Copyright (c) 2023 Viresh Ratnakar
See the full license notice in exolve-m.js.
*/
/**
* Constructing an ExolveEmbedder object does all the work of
* decoding the URL parameters to find a puzzle file and serve it.
*/
class ExolveEmbedder {
/**
* If dirPrefix is specified, it is prepended to the crossword
* path before reading the file. If titlePrefix is specified, it
* is prepended to the puzzle title (or "Untitled" if puzzle title
* is missing) and set as the document title.
*/
constructor(dirPrefix='', titlePrefix='') {
this.dirPrefix = dirPrefix;
this.titlePrefix = titlePrefix;
this.embedder = document.getElementById('xlv-embedder');
this.crossword = '';
this.exolveOverrides = '';
const urlParams = new URLSearchParams(window.location.search);
for (const [key, value] of urlParams) {
const decodedValue = decodeURIComponent(value);
if (key == 'crossword' || key == 'xwd') {
if (this.crossword) {
throw new Error('Multiple "crossword=" or "xwd=" keys found.');
}
this.crossword = decodedValue;
continue;
}
this.exolveOverrides += 'exolve-' + key + ': ' + decodedValue + '\n';
}
if (!this.crossword) {
throw new Error('Must specify "?crossword=[puz/ipuz/exolve file]" (or ?xwd=...) in the URL.');
}
this.crossword = dirPrefix + this.crossword;
const finisher = this.showData.bind(this);
fetch(this.crossword, {
mode: 'cors',
credentials: 'include',
}).then(response => {
if (!response.ok) {
throw new Error(
'Error fetching [' + this.crossword +
`], status = ${response.status}`);
}
return response.arrayBuffer();
}).then(result => {
finisher(result);
}).catch(error => {
this.showMessage(error.name + ': ' + error.message);
});
}
/**
* Returns true if the crossword was successfully shown.
*/
showExolve(specs) {
let start = specs.indexOf('exolve-begin')
let end = specs.indexOf('exolve-end')
if (start < 0 || end < 0 || start >= end) {
return false;
}
while (start > 0 && specs.charAt(start - 1) == ' ') {
start--;
}
const exolveSpecs = specs.substring(start, end) +
this.exolveOverrides + 'exolve-end';
const notInIframe = (window === window.parent);
try {
const xlv = new Exolve(exolveSpecs, 'xlv-embedder', null, notInIframe);
if (this.titlePrefix) {
document.title = this.titlePrefix + (xlv.title || 'Untitled');
}
} catch (err) {
this.showMessage(err);
return false;
}
return true;
}
showIpuz(specs) {
let start = specs.indexOf('{')
let end = specs.lastIndexOf('}')
if (start < 0 || end < 0 || start >= end) {
return false;
}
const ipuzJSON = specs.substring(start, end) + '}';
try {
const ipuz = JSON.parse(ipuzJSON);
const exolve = exolveFromIpuz(ipuz, this.crossword);
if (!exolve) {
return false;
}
return this.showExolve(exolve);
} catch (err) {
console.log(err);
}
return false;
}
showPuz(buffer) {
const exolve = exolveFromPuz(buffer, this.crossword);
if (!exolve) {
return false;
}
return this.showExolve(exolve);
}
showData(data) {
const utf8decoder = new TextDecoder();
const text = utf8decoder.decode(data);
if (this.showExolve(text)) {
return;
}
if (this.showIpuz(text)) {
return;
}
if (this.showPuz(data)) {
return;
}
this.showMessage('Could not interpret the data in [' + this.crossword + '] as a crossword.');
}
showMessage(msg) {
this.embedder.innerHTML = msg;
}
}