Skip to content

Commit

Permalink
Resolve #3
Browse files Browse the repository at this point in the history
- Bump version to v1.7
  • Loading branch information
osk2 committed Sep 17, 2018
1 parent 7b113c2 commit 343d6e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
47 changes: 38 additions & 9 deletions chrome-extension/js/content_script.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
(async () => {
try {
const HOST = 'https://osk2.me:9977';
const ipValidation = /((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/;
const comments = document.querySelectorAll('.push-ipdatetime');
const ipList = [].map.call(comments, node => node.textContent.trim().split(' ')[0]);
const flagsResponse = await axios.post(`${HOST}/ip`, { ip: ipList });
const flags = flagsResponse.data;
const f2Nodes = document.querySelectorAll('.f2');
const authorComment = [].filter.call(f2Nodes, n => n.innerHTML.match(ipValidation));
const authorIpList = authorComment.map(c => c.innerHTML.match(ipValidation)[0]);
const commentIpList = [].map.call(comments, n => n.textContent.trim().split(' ')[0]);
const authorFlagsResponse = await axios.post(`${HOST}/ip`, { ip: authorIpList });
const authorFlags = authorFlagsResponse.data;

const generateImageHTML = (ip, flag) => {
const imagePath = flag.imagePath ? `${HOST}/${flag.imagePath}` : null;
const imageTitile = `${flag.locationName || 'N/A'}<br>
<a href='https://www.google.com/search?q=${ip}' target='_blank'>${ip}</a>`;

if (!imagePath) {
return;
}
return `<img data-flag src="${imagePath}" title="${imageTitile}">`;
}

authorComment.forEach((comment, index) => {
const ip = comment.innerHTML.match(ipValidation)[0];
const imageHTML = generateImageHTML(ip, authorFlags[index]);

if (!imageHTML) {
return;
}
comment.innerHTML = comment.innerHTML.replace(ipValidation, `${imageHTML} ${ip}`);
});

if (commentIpList.lenth === 0) {
return;
}

const commentFlagsResponse = await axios.post(`${HOST}/ip`, { ip: commentIpList });
const flags = commentFlagsResponse.data;

[].forEach.call(comments, (comment, index) => {
const commentInfo = comment.textContent.trim().split(' ');
const commentIp = commentInfo[0];
const commentId = comment.previousSibling.previousSibling.innerHTML;
const commentDatetime = commentInfo.splice(1, 2).join(' ');
const imagePath = flags[index].imagePath ? `${HOST}/${flags[index].imagePath}` : null;
const imageTitile = `${flags[index].locationName || 'N/A'}<br>
<a href='https://www.google.com/search?q=${commentId}+${commentIp}' target='_blank'>${commentIp}</a>`;
const imageHTML = `<img data-flag src="${imagePath}" title="${imageTitile}">`;
const imageHTML = generateImageHTML(commentIp, flags[index]);

if (!imagePath) {
if (!imageHTML) {
return;
}
comment.innerHTML = `${imageHTML} ${commentDatetime}`;
});

tippy('[data-flag]', {
arrow: true,
size: 'large',
Expand Down
2 changes: 1 addition & 1 deletion chrome-extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "八卦插IP",
"short_name": "八卦插IP",
"manifest_version": 2,
"version": "1.6",
"version": "1.7",
"description": "開門!查IP",
"author": "osk2",
"icons": { "128": "images/icon_128.png" },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ptt-comment-flag",
"version": "1.0.0",
"version": "1.7",
"description": "Query country of PTT comment",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 343d6e8

Please sign in to comment.