Compare commits
30
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33180c8be8 | ||
|
|
ba10fd2573 | ||
|
|
922f2e02ec | ||
|
|
fcbfeeb96a | ||
|
|
d8089d9cb3 | ||
|
|
a18f991c0b | ||
|
|
cc264daa79 | ||
|
|
8521c1dc6e | ||
|
|
d26a74ff3d | ||
|
|
2a85492489 | ||
|
|
e8c3e53685 | ||
|
|
0d6aac7844 | ||
|
|
0d0d06d265 | ||
|
|
66b4374b48 | ||
|
|
bdac2ca9a0 | ||
|
|
99579ba7e5 | ||
|
|
be035bf478 | ||
|
|
ce27c52a48 | ||
|
|
86f2b7c34f | ||
|
|
c688761e89 | ||
|
|
174e2dd1cd | ||
|
|
a9ca40c2bd | ||
|
|
eae5d1cc25 | ||
|
|
1d8dfdb1da | ||
|
|
6c268aa829 | ||
|
|
73a8b4f78f | ||
|
|
1f7cfd637a | ||
|
|
02a1482789 | ||
|
|
60b514eae7 | ||
|
|
dedeeb77a8 |
+10
-66
@@ -1,7 +1,8 @@
|
||||
window.EditorMovie = (function () {
|
||||
'use strict';
|
||||
|
||||
var STORAGE_KEY = 'omdb-api-key';
|
||||
// OMDb API key. Shipped to the browser; acceptable for a single-user LAN tool.
|
||||
var OMDB_API_KEY = 'c906744f';
|
||||
|
||||
var BEGIN = '<!-- BEGIN MOVIE -->';
|
||||
var END = '<!-- END MOVIE -->';
|
||||
@@ -72,8 +73,8 @@ window.EditorMovie = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
function fetchMovie(key, title, year) {
|
||||
var url = 'https://www.omdbapi.com/?apikey=' + encodeURIComponent(key) +
|
||||
function fetchMovie(title, year) {
|
||||
var url = 'https://www.omdbapi.com/?apikey=' + encodeURIComponent(OMDB_API_KEY) +
|
||||
'&type=movie&t=' + encodeURIComponent(title);
|
||||
if (year) url += '&y=' + encodeURIComponent(year);
|
||||
return fetch(url).then(function (r) { return r.json(); });
|
||||
@@ -83,54 +84,17 @@ window.EditorMovie = (function () {
|
||||
openModal({ title: title, body: msg, confirm: { label: 'OK' } });
|
||||
}
|
||||
|
||||
function promptForKey(rejected, onSaved) {
|
||||
var body = document.createDocumentFragment();
|
||||
|
||||
if (rejected) {
|
||||
var notice = document.createElement('p');
|
||||
notice.textContent = 'The previously stored key was rejected by OMDb.';
|
||||
body.appendChild(notice);
|
||||
function run(textarea) {
|
||||
if (!OMDB_API_KEY) {
|
||||
showMessage('Movie import', 'OMDb API key is not set. Edit assets/editor/movie.js.');
|
||||
return;
|
||||
}
|
||||
|
||||
var info = document.createElement('p');
|
||||
info.appendChild(document.createTextNode('Enter your OMDb API key. Get one at '));
|
||||
var link = document.createElement('a');
|
||||
link.href = 'https://www.omdbapi.com/apikey.aspx';
|
||||
link.target = '_blank';
|
||||
link.rel = 'noopener';
|
||||
link.textContent = 'omdbapi.com/apikey.aspx';
|
||||
info.appendChild(link);
|
||||
info.appendChild(document.createTextNode('.'));
|
||||
body.appendChild(info);
|
||||
|
||||
var input = document.createElement('input');
|
||||
input.type = 'text';
|
||||
input.className = 'modal-input';
|
||||
input.placeholder = 'OMDb API key';
|
||||
body.appendChild(input);
|
||||
|
||||
openModal({
|
||||
title: 'OMDb API key required',
|
||||
body: body,
|
||||
confirm: {
|
||||
label: 'SAVE',
|
||||
onConfirm: function () {
|
||||
var key = input.value.trim();
|
||||
if (!key) return;
|
||||
localStorage.setItem(STORAGE_KEY, key);
|
||||
closeModal();
|
||||
onSaved(key);
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function importWithKey(textarea, key, initialTitle) {
|
||||
var input = document.createElement('input');
|
||||
input.type = 'text';
|
||||
input.className = 'modal-input';
|
||||
input.placeholder = 'Title, optionally with (YYYY)';
|
||||
input.value = initialTitle;
|
||||
input.value = firstHeading(textarea.value || '');
|
||||
|
||||
openModal({
|
||||
title: 'Import movie',
|
||||
@@ -142,16 +106,8 @@ window.EditorMovie = (function () {
|
||||
if (!raw) return;
|
||||
var parsed = parseTitleYear(raw);
|
||||
closeModal();
|
||||
fetchMovie(key, parsed.title, parsed.year)
|
||||
fetchMovie(parsed.title, parsed.year)
|
||||
.then(function (data) {
|
||||
if (data && data.Response === 'False' &&
|
||||
data.Error === 'Invalid API key!') {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
promptForKey(true, function (newKey) {
|
||||
importWithKey(textarea, newKey, raw);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!data || data.Response === 'False') {
|
||||
showMessage('Not found',
|
||||
(data && data.Error) || 'Movie not found.');
|
||||
@@ -167,17 +123,5 @@ window.EditorMovie = (function () {
|
||||
});
|
||||
}
|
||||
|
||||
function run(textarea) {
|
||||
var initialTitle = firstHeading(textarea.value || '');
|
||||
var key = localStorage.getItem(STORAGE_KEY);
|
||||
if (!key) {
|
||||
promptForKey(false, function (newKey) {
|
||||
importWithKey(textarea, newKey, initialTitle);
|
||||
});
|
||||
return;
|
||||
}
|
||||
importWithKey(textarea, key, initialTitle);
|
||||
}
|
||||
|
||||
return { run: run };
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user