Skip to content

Instantly share code, notes, and snippets.

@thrakt
Created March 30, 2011 13:35
Show Gist options
  • Save thrakt/894396 to your computer and use it in GitHub Desktop.
Save thrakt/894396 to your computer and use it in GitHub Desktop.
/*
* pixiv Bookmark - Tombloo patches
*
* ピクシブでブックマークするパッチ
*
* 同時に「お気に入りユーザーに追加」する場合は
* 下の BOOKMARK_USER 定義を true にする
*
* Based: http://gist.github.com/318137
* Version 1.03, 2011-03-05 polygon planet <http://polygonplanet.tumblr.com/>
*/
(function() {
// お気に入り (ユーザー) 追加の ON / OFF
const BOOKMARK_USER = false;
models.register(update({
name: 'pixiv Bookmark',
ICON: 'http://www.pixiv.net/favicon.ico',
BASE_URL: 'http://www.pixiv.net/',
ILLUST_PAGE_REGEXP: /^http:\/\/www\.pixiv\.net\/member_illust\.php/,
MANGA_PAGE_REGEXP: /\b(?:mode=manga)\b/,
check: function(ps) {
return /(?:photo|link)/.test(ps.type) &&
ps.pageUrl.match(this.ILLUST_PAGE_REGEXP);
},
isMangaPage: function(url) {
return this.ILLUST_PAGE_REGEXP.test(url) &&
this.MANGA_PAGE_REGEXP.test(url);
},
getToken: function() {
var status = this.updateSession();
switch (status) {
case 'none':
throw new Error(getMessage('error.notLoggedin'));
case 'same':
if (this.token) {
return succeed(this.token);
}
//break;
case 'changed':
var self = this;
return request(self.BASE_URL + 'bookmark.php').
addCallback(function(res) {
return self.token = $x(
'//form[@id="f"]/input[@name="tt"]/@value',
convertToHTMLDocument(res.responseText)
);
});
}
},
getAuthCookie: function() {
//FIXME: ログイン状態の判別が出来てないかも(?)
return getCookieString('pixiv.net', 'PHPSESSID');
},
getDocument: function() {
var doc = null;
try {
doc = getMostRecentWindow().content.document;
if (!doc) {
doc = currentDocument && currentDocument() || document;
}
if (doc.domain !== 'www.pixiv.net') {
throw doc;
}
} catch (e) {
doc = null;
}
return doc;
},
fixBookmarkPageLabel: function(node) {
var i, s, v, c, a = node.getElementsByTagName('a');
for (i = 0; i < a.length; i++) {
try {
s = v = a[i].innerHTML;
c = s.charCodeAt(0);
if (c !== 0x30d6 && c <= 0xff) {
v = decodeURIComponent(escape(s));
}
} catch (er) {
v = s;
}
a[i].innerHTML = v;
}
return node.innerHTML;
},
/**
* ブックマークに追加
*/
addBookmarkPage: function(ps, doc, addUrl) {
var self = this, token = self.getToken(), psc = ps;
if (!token) {
throw new Error('[addBookmarkPage] Cannot get token');
}
return token.addCallback(function(token) {
return request(addUrl, {
sendContent: {
mode: 'add',
tt: token,
id: psc.pageUrl.match(/illust_id=(\d+)/)[1],
type: 'illust',
restrict: 0, // 1 = 非公開, 0 = 公開
tag: joinText(ps.tags || [], ' '),
comment: joinText([psc.body, psc.description], ' ', true)
}
}).addCallback(function(res) {
request(psc.pageUrl).addCallback(function(response) {
var oldNode, newNode, html, selector, label;
try {
selector = '.works_illusticonsBlock div:last-child span';
html = convertToHTMLDocument(response.responseText);
newNode = html.querySelector(selector);
oldNode = doc.querySelector(selector);
if (newNode && oldNode) {
//FIXME: まれにデコード未処理で返ってくる
label = self.fixBookmarkPageLabel(newNode);
oldNode.innerHTML = label || newNode.innerHTML;
oldNode.removeAttribute('class');
}
} catch (e) {
throw new Error(
'[addBookmarkPage] Update failed: ' + e.message
);
}
});
});
});
},
/**
* お気に入り追加 (ユーザー)
*/
addBookmarkUser: function(ps, doc, addUrl) {
if (!BOOKMARK_USER) {
return;
}
var self = this, token = self.getToken(), psc = ps;
if (!token) {
throw new Error('[addBookmarkUser] Cannot get token');
}
return token.addCallback(function(token) {
var getValue = function(name) {
return $x(
'//*[@id="favorite-preference"]' +
'//input[@type="hidden"][@name="' + name + '"]/@value',
doc
);
};
return request(addUrl, {
sendContent: {
mode: 'add',
tt: token,
type: 'user',
user_id: getValue('user_id'),
restrict: 0, // 1 = 非公開, 0 = 公開
left_column: 'OK'
}
}).addCallback(function(res) {
request(psc.pageUrl).addCallback(function(response) {
var oldNode, newNode, html, selector;
try {
selector = '#favorite-container';
html = convertToHTMLDocument(response.responseText);
newNode = html.querySelector(selector);
oldNode = doc.querySelector(selector);
if (newNode && oldNode) {
// イベントはコピーしないので見た目だけ
oldNode.innerHTML = newNode.innerHTML;
}
} catch (e) {
throw new Error(
'[addBookmarkUser] Update failed: ' + e.message
);
}
});
});
});
},
post: function(ps) {
var self = this, token, ret, doc = self.getDocument(), xpath,
psc = update({}, ps), addUrl = self.BASE_URL + 'bookmark_add.php';
if (doc && !self.isMangaPage(psc.pageUrl)) {
// ブックマーク
xpath = '//*[contains(@class,"works_illusticonsBlock")]//div[last()]' +
'/span/a[last()=1][contains(@href,"bookmark_add")]';
if ( $x(xpath, doc) ) {
ret = self.addBookmarkPage(psc, doc, addUrl);
}
// お気に入り追加 (ユーザー)
if (BOOKMARK_USER) {
xpath = '//*[@id="favorite-preference"]' +
'//form[contains(@action,"bookmark_add")]';
if ( $x(xpath, doc) ) {
self.addBookmarkUser(psc, doc, addUrl);
}
}
}
return ret;
}
}, AbstractSessionService));
})();
@thrakt
Copy link
Author

thrakt commented Mar 30, 2011

要素が変わってブックマークできなくなっていたので、
ブックマークの所を見に行くxpathとselectorを修正しました。
動いているのは確認しているので大丈夫のはず・・・

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment