1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PhontomJSでココログのコメントをチェックする

Last updated at Posted at 2012-12-07

動機

7月あたりからココログにコメントをいただいていたが、
ガン無視していたことになってしまった。

そんなひどい奴が管理してるブログはこちら

http://kjunichi.cocolog-nifty.com/misc/

ソース

ひどいソースだが、PhantomJSでの画面遷移の備忘録も兼ねて、さらしておきます。

cocoCmt.js
var COCOLOG_ID = "your id";
var COCOLOG_PWD = "your password";
var COCOLOG_BLOG_ID = "your blog id"

var page = require('webpage').create();

var url = "https://login.nifty.com/service/logout?s=cocoapp&subs=cocolog";
var counter = 0;
var loginCnt = 0;
page.open(url, function (status) {
//console.log("page.open status = "+status + ", url = "+page.url);
//page.render("cocologin_"+(counter++)+".png");
if(status != "success") {
//console.log("0:network error "+status);
        phantom.exit(1);
}
//console.log("ans = "+page.url.indexOf("https://login.nifty.com/service/logout?s=cocoapp&subs=cocolog"));
if(page.url.indexOf("https://login.nifty.com/service/logout?s=cocoapp&subs=cocolog") > -1) {
// ログアウト画面
//console.log("logout start");
//page.render("cocologin0.png");
url="https://login.nifty.com/service/login?s=cocoapp";
page.evaluate(function() {
location.href="https://login.nifty.com/service/login?s=cocoapp";
});
//console.log("logout end");
return;
}
//console.log("ans2 = "+page.url.indexOf("https://login.nifty.com/service/login?s=cocoapp"));
if(page.url.indexOf("https://login.nifty.com/service/login?s=cocoapp") == 0) {
//if(loginCnt > 0) {
//console.log("start login");
//page.render("cocologin.png");

var result = evaluate(page,function (id,pwd) {
if(document.getElementById("username")==null) {
return "false";
}
document.getElementById("username").value = id;
     document.getElementById("password").value = pwd;
         document.getElementById("form1").submit();
         return "success";
     },COCOLOG_ID,COCOLOG_PWD);
     //console.log("login : "+result);
     //page.render("cocologin2.png");
//console.log("end login");

//}
//loginCnt++;
return;
}
//console.log("ans 3 = " + page.url.indexOf("http://app.cocolog-nifty.com/t/app/"));

if(page.url.indexOf("http://app.cocolog-nifty.com/t/app/weblog/post?__mode=list_comments&blog_id=") == 0) {
//page.render("cocologComment.png");
var cmtTbl = page.evaluate(function(){
return document.getElementsByClassName("tableselect")[0].innerHTML;
});
console.log(cmtTbl);
phantom.exit(0);
}
if(page.url.indexOf("http://app.cocolog-nifty.com/t/app/") == 0) {
//console.log("http://app.cocolog-nifty.com/t/app/");
evaluate(page,function(blog_id) {
location.href="http://app.cocolog-nifty.com/t/app/weblog/post?__mode=list_comments&blog_id="+blog_id;
},COCOLOG_BLOG_ID);
return;
}

});

/*
* This function wraps WebPage.evaluate, and offers the possibility to pass
* parameters into the webpage function. The PhantomJS issue is here:
*
* http://code.google.com/p/phantomjs/issues/detail?id=132
*
* This is from comment #43.
*/
function evaluate(page, func) {
    var args = [].slice.call(arguments, 2);
    var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
    return page.evaluate(fn);
}

はめられた事

waitfor.js

exampleに入っているが、これ動かねぇじゃねぇか!!

page.openは何度も呼ばれる

location.hrefやsubmitしたらその都度呼ばれる。
これに気が付かないと今回の実装は出来なかった。

phantom1.7以降が楽チン

page.urlが使えないので、俺のスキルでは実現が難しそう。。

参考ページ

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?