Skip to content

Commit

Permalink
Use lgtmoon.dev instead of LGTM.in
Browse files Browse the repository at this point in the history
  • Loading branch information
fukayatsu committed Nov 22, 2024
1 parent fb64313 commit 1f0465b
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 159 deletions.
4 changes: 4 additions & 0 deletions lib/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"48": "images/icon_48.png",
"128": "images/icon_128.png"
},
"host_permissions": ["https://lgtmoon.dev/api/images/random"],
"background": {
"service_worker": "service_worker.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
Expand Down
305 changes: 146 additions & 159 deletions src/js/lttm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,182 +19,169 @@ const atwhoOptions = {
},

remote_filter(query, callback) {
let url;
if (!query) { return; }

const kind = query[0].toLowerCase();
query = query.slice(1);
switch (false) {
case kind !== "l":
if (location.protocol === "https:") {
url = 'https://lttm-ssl.herokuapp.com/lgtm';
} else {
url = 'http://www.lgtm.in/g';
}
var task1 = $.getJSON(url + '?' + Math.random()).then();
var task2 = $.getJSON(url + '?' + Math.random()).then();
var task3 = $.getJSON(url + '?' + Math.random()).then();
return $.when(task1, task2, task3).then(function(a, b, c) {
const images = _.map([
a[0],
b[0],
c[0]
], function(data) {
const imageUrl = data.actualImageUrl;
return {
name: imageUrl,
imageUrl,
imagePreviewUrl: previewUrl(imageUrl),
alt: "LGTM"
};

if (kind == "l") {
chrome.runtime.sendMessage({ action: 'getJSON', url: 'https://lgtmoon.dev/api/images/random' }, function (response) {
const images = [];
$.each(response.images, function(k, v) {
images.push({
name: v.url,
imageUrl: v.url,
imagePreviewUrl: v.url,
alt: "lgtm"
});
return callback(images);
});
case kind !== "t":
if (query) {
return $.getJSON("https://d942scftf40wm.cloudfront.net/search.json",
{q: query}
, function(data) {
const images = [];
$.each(data, function(k, v) {
url = "https://img.tiqav.com/" + v.id + "." + v.ext;
return images.push({
name: url,
imageUrl: url,
imagePreviewUrl: previewUrl(url),
alt: "tiqav"
});
});
return callback(images);
});
}
break;
case kind !== "m":
return $.getJSON(chrome.runtime.getURL("/config/meigens.json"), function(data) {
let boys = [];
if (query) {
boys = _.filter(data, n => (n.title && (n.title.indexOf(query) > -1)) || (n.body && (n.body.indexOf(query) > -1)));
} else {
boys = _.sample(data, 30);
}
callback(images);
});
} else if (kind == "t") {
if (query) {
return $.getJSON("https://d942scftf40wm.cloudfront.net/search.json",
{q: query}
, function(data) {
const images = [];
$.each(boys, function(k, v) {
const image = v.image.replace('http://', 'https://');
$.each(data, function(k, v) {
const url = "https://img.tiqav.com/" + v.id + "." + v.ext;
return images.push({
name: image,
imageUrl: image,
imagePreviewUrl: previewUrl(image),
alt: "ミサワ"
name: url,
imageUrl: url,
imagePreviewUrl: previewUrl(url),
alt: "tiqav"
});
});
return callback(images);
callback(images);
});
case kind !== "i":
return $.getJSON(chrome.runtime.getURL("/config/irasutoya.json"), function(data) {
let illustrations = [];
if (query) {
illustrations = _.filter(data, n => ((n.title != null ? n.title.indexOf(query) : undefined) > -1) ||
((n.description != null ? n.description.indexOf(query) : undefined) > -1) ||
(n.categories && (n.categories.join().indexOf(query) > -1)));
} else {
illustrations = _.sample(data, 30);
}
const images = [];
$.each(illustrations, function(k, v) {
const image_url = v.image_url.replace('http://', 'https://');
return images.push({
name: image_url,
imageUrl: image_url,
imagePreviewUrl: previewUrl(image_url),
alt: v.title
});
}
} else if (kind == "m") {
return $.getJSON(chrome.runtime.getURL("/config/meigens.json"), function(data) {
let boys = [];
if (query) {
boys = _.filter(data, n => (n.title && (n.title.indexOf(query) > -1)) || (n.body && (n.body.indexOf(query) > -1)));
} else {
boys = _.sample(data, 30);
}
const images = [];
$.each(boys, function(k, v) {
const image = v.image.replace('http://', 'https://');
return images.push({
name: image,
imageUrl: image,
imagePreviewUrl: previewUrl(image),
alt: "ミサワ"
});
return callback(images);
});
case kind !== 's':
return $.getJSON(chrome.runtime.getURL("/config/sushi_list.json"), function(data) {
let sushiList = [];
if (query) {
sushiList = _.filter(data, sushi => !!_.find(sushi.keywords, keyword => keyword.indexOf(query) > -1));
} else {
sushiList = data;
}

const images = [];
_.each(sushiList, sushi => images.push({
name: sushi.url,
imageUrl: sushi.url,
imagePreviewUrl: sushi.url,
alt: `寿司ゆき:${sushi.keywords[0]}`}));
return callback(images);
callback(images);
});
} else if (kind == "i") {
return $.getJSON(chrome.runtime.getURL("/config/irasutoya.json"), function(data) {
let illustrations = [];
if (query) {
illustrations = _.filter(data, n => ((n.title != null ? n.title.indexOf(query) : undefined) > -1) ||
((n.description != null ? n.description.indexOf(query) : undefined) > -1) ||
(n.categories && (n.categories.join().indexOf(query) > -1)));
} else {
illustrations = _.sample(data, 30);
}
const images = [];
$.each(illustrations, function(k, v) {
const image_url = v.image_url.replace('http://', 'https://');
return images.push({
name: image_url,
imageUrl: image_url,
imagePreviewUrl: previewUrl(image_url),
alt: v.title
});
});
case kind !== 'j':
return $.getJSON(chrome.runtime.getURL("/config/js_girls.json"), function(data) {
let js_girls = [];
if (query) {
js_girls = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
js_girls = data;
}
callback(images);
});
} else if (kind == 's') {
return $.getJSON(chrome.runtime.getURL("/config/sushi_list.json"), function(data) {
let sushiList = [];
if (query) {
sushiList = _.filter(data, sushi => !!_.find(sushi.keywords, keyword => keyword.indexOf(query) > -1));
} else {
sushiList = data;
}

const images = [];
_.each(js_girls, js_girl => images.push({
name: js_girl.url,
imageUrl: js_girl.url,
imagePreviewUrl: js_girl.url,
alt: `JS Girls:${js_girl.keywords[0]}`}));
return callback(images);
});
case kind !== 'n':
return $.getJSON(chrome.runtime.getURL("/config/engineer_homeru_neko.json"), function(data) {
let source = [];
if (query) {
source = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
source = data;
}
const images = [];
_.each(sushiList, sushi => images.push({
name: sushi.url,
imageUrl: sushi.url,
imagePreviewUrl: sushi.url,
alt: `寿司ゆき:${sushi.keywords[0]}`}));
callback(images);
});
} else if (kind == 'j') {
return $.getJSON(chrome.runtime.getURL("/config/js_girls.json"), function(data) {
let js_girls = [];
if (query) {
js_girls = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
js_girls = data;
}

const images = [];
_.each(source, item => images.push({
name: item.url,
imageUrl: item.url,
imagePreviewUrl: item.url,
alt: `エンジニアを褒めるネコ:${item.keywords[0]}`}));
return callback(images);
});
case kind !== 'd':
return $.getJSON(chrome.runtime.getURL("/config/decomoji.json"), function(data) {
let decomojis = [];
if (query) {
decomojis = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
decomojis = data;
}
const images = [];
_.each(js_girls, js_girl => images.push({
name: js_girl.url,
imageUrl: js_girl.url,
imagePreviewUrl: js_girl.url,
alt: `JS Girls:${js_girl.keywords[0]}`}));
callback(images);
});
} else if (kind == 'n') {
return $.getJSON(chrome.runtime.getURL("/config/engineer_homeru_neko.json"), function(data) {
let source = [];
if (query) {
source = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
source = data;
}

const images = [];
_.each(decomojis, decomoji => images.push({
name: decomoji.url,
imageUrl: decomoji.url,
imagePreviewUrl: decomoji.url,
alt: `:${decomoji.keywords[0]}`}));
return callback(images);
});
case kind !== 'r':
return $.getJSON(chrome.runtime.getURL("/config/sushidot.json"), function(data) {
let sushidots = [];
if (query) {
sushidots = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
sushidots = data;
}
const images = [];
_.each(source, item => images.push({
name: item.url,
imageUrl: item.url,
imagePreviewUrl: item.url,
alt: `エンジニアを褒めるネコ:${item.keywords[0]}`}));
callback(images);
});
} else if(kind == 'd') {
return $.getJSON(chrome.runtime.getURL("/config/decomoji.json"), function(data) {
let decomojis = [];
if (query) {
decomojis = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
decomojis = data;
}

const images = [];
_.each(sushidots, sushidot => images.push({
name: sushidot.url,
imageUrl: sushidot.url,
imagePreviewUrl: sushidot.url,
alt: `:${sushidot.keywords[0]}`}));
return callback(images);
});
const images = [];
_.each(decomojis, decomoji => images.push({
name: decomoji.url,
imageUrl: decomoji.url,
imagePreviewUrl: decomoji.url,
alt: `:${decomoji.keywords[0]}`}));
callback(images);
});
} else if (kind == 'r') {
return $.getJSON(chrome.runtime.getURL("/config/sushidot.json"), function(data) {
let sushidots = [];
if (query) {
sushidots = _.filter(data, js_girl => !!_.find(js_girl.keywords, keyword => keyword.indexOf(query) > -1));
} else {
sushidots = data;
}

const images = [];
_.each(sushidots, sushidot => images.push({
name: sushidot.url,
imageUrl: sushidot.url,
imagePreviewUrl: sushidot.url,
alt: `:${sushidot.keywords[0]}`}));
callback(images);
});
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/js/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action == 'getJSON') {
fetch(request.url).then(function (response) {
return response.json();
}).then(function (json) {
sendResponse(json);
});

// https://www.mitsuru-takahashi.net/blog/chrome-extension-response/
// https://stackoverflow.com/questions/48107746/chrome-extension-message-not-sending-response-undefined
return true;
} else {
console.log('unsoported action', request.action);
}
})

0 comments on commit 1f0465b

Please sign in to comment.