Skip to content

Instantly share code, notes, and snippets.

@shtrih
Last active August 29, 2015 14:00
Show Gist options
  • Save shtrih/11362381 to your computer and use it in GitHub Desktop.
Save shtrih/11362381 to your computer and use it in GitHub Desktop.
Habrahabr comments to PHP Array
// ==UserScript==
// @name Habr comments2array
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match http://habrahabr.ru/post/*
// @match http://habrahabr.ru/company/*/blog/*
// @copyright 2014+, shtrih
// ==/UserScript==
// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")(jQ);";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// the guts of this userscript
function main($) {
var $comment_list = $('.comments_list'),
$comment_item = $('.comment_item', $comment_list),
reply_comments = '.reply_comments',
result_structure = {
"clone": function() {
return JSON.parse(JSON.stringify(this));
},
"toString": function () {
var temp = '\n\tarray(\n';
for (key in this) {
if (this.hasOwnProperty(key) && key !== 'toString') {
temp += "\t\t'" + key + "' => " + (function (key, value) {
if (key == 'pictures' || key == 'files' || typeof value == "boolean")
return value;
return "'" + value + "'";
})(key, this[key]) + ",\n";
}
}
return temp + '\n\t)';
},
"id": "235",
"parent_id": "0",
"module": "3",
"item_id": "14",
"item_type": "c",
"is_deleted": "0",
"desc": "Memoria est signatarum rerum in mente vestigium",
"add_date": "2012-10-24 08:14:00",
"mod_date": "0000-00-00 00:00:00",
"mod_count": "0",
"mod_user": "0",
"author": "0",
"author_name": "",
"author_mail": "[email protected]",
"is_can_answer": "0",
/*"author_user": {
"id": "1",
"name": "i-deal Studio",
"password": "24852a71763901ac14dd1d52bc94a6f3",
"mail": "[email protected]",
"active": "1",
"locked": "0",
"timezone_name": "",
"date_format": "0",
"subscribe": " ",
"register_date": "0000-00-00 00:00:00",
"lastaction_date": "2013-10-30 09:49:38",
"lastvisited_url": "\/about",
"last_ip": "192.168.1.11",
"company": "1",
"name1": "",
"name2": "",
"name3": "",
"gender": "0",
"birthdate": "0000-00-00",
"region": "0",
"site": "",
"real_address": "",
"urid_address": "",
"phones": "",
"contacts": "",
"bank": "",
"ext_info": "",
"inn_kpp": "",
"other": ""
},*/
"can_edit": true,
"can_reply": "0",
"pictures": " array(\r\n\t\t\t'can_edit' => true,\r\n\t\t\t'items' => array(),\r\n\t\t\t'use_photo_page' => '1'\n\t\t)",
"files": " array(\r\n\t\t\t'can_edit' => true,\r\n\t\t\t'items' => array()\n\t\t)"
},
result = [],
html_prepress = function (html) {
if (html)
return html.replace(/'/g, "\\'")
.replace(/\n/g, '')
.replace(/<(br|\/li)>/g, '\n')
.replace(/^\s*|\s*$/, '')
.replace(/<a\s*href="([^"]*)"[^>]*>([^<]*)<\/a>/g, '$2 ($1)')
.replace(/<img\s*src="([^"]*)"[^>]*\/?>/g, '$1 ')
.replace(/<\/?[^>]+(>|$)/g, "")
;
return html;
}
;
$comment_item.each(function () {
var self = $(this),
comment = result_structure.clone(),
add_date = $('time', self).first().text().split(' ')
;
// НЛО прилетело и опубликовало эту надпись здесь
if (add_date.length == 1)
return;
comment.toString = result_structure.toString;
comment.parent_id = $('.parent_id', self).first().data('parent_id');
comment.id = $('.info', self).first().attr('rel');
comment.author_name = $('.username', self).first().text();
comment.author_mail = comment.author_name + '@i-dl.ru';
comment.add_date = add_date[2] + '-'
+ add_date[1]
.replace(/^янв.*/, '01')
.replace(/^фев.*/, '02')
.replace(/^мар.*/, '03')
.replace(/^апр.*/, '04')
.replace('мая', '05')
.replace(/^июн.*/, '06')
.replace(/^июл.*/, '07')
.replace(/^авг.*/, '08')
.replace(/^сен.*/, '09')
.replace(/^окт.*/, '10')
.replace(/^ноя.*/, '11')
.replace(/^дек.*/, '12')
+ '-' + add_date[0] + ' ' + add_date[4] + ':00'; // 25 апреля 2014 в 14:32
comment.desc = html_prepress( $('.message', self).first().html() );
//comment.can_edit = !!$('.info.is_topic_starter').length;
if (typeof result[comment.parent_id] == 'undefined')
result[comment.parent_id] = [];
result[comment.parent_id].push(comment);
});
var temp = 'array(';
for (key in result) {
if (result.hasOwnProperty(key)) {
temp += "\n'" + key + "' => array(\n\t\t" + result[key].toString() + "\n),";
}
}
console.log(
temp + '\n);'
);
}
// load jQuery and execute the main function
addJQuery(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment