Skip to content

Instantly share code, notes, and snippets.

@yamaaki
Last active August 29, 2015 14:25
Show Gist options
  • Save yamaaki/0b96f1bef8c16feac244 to your computer and use it in GitHub Desktop.
Save yamaaki/0b96f1bef8c16feac244 to your computer and use it in GitHub Desktop.
// turbolinks で画面遷移したときにコールされる (reload 時にはコールされない)
// DOM へのイベントのバインドは turbolinks だと毎回バインドしなおす必要があるのでコチラで
// 何度も呼ばれる場合があるので .off() してから .on() する
bindClick = function () {
$('.p-link').off('click').on('click', function (e) {
var that = $(this)
, params = {}
// 以下の構成の場合、i が e.target になるので i に p-no-jump を add
// .p-link
// a data-method="delete"
// i.material-icons.p-no-jump send
// ここで e.preventDefault() や e.stopPropagation() をすると
// data-method="delete" が効かなくなる
if($(e.target).hasClass('p-no-jump')) {
return true;
}
// (1) サーバー内の Capybara からセッション情報を取得
params.email = that.attr('data-email');
$.getJSON('/api/v1/cookies', params, function (data) {
// (2) それを background.js に伝達
chrome.runtime.sendMessage({
href: that.attr('data-href'),
cookies: data.response.cookies
});
});
})
}
$(document).on('page:load', function () {
bindClick();
})
// reload 時に一度だけコールされる
$(document).ready(function () {
bindClick();
// 通常の JavaScript からのイベントを受ける
document.addEventListener('execBindClick', function(data) {
bindClick();
});
// (6) background.js から戻ってきた処理を受けてログイン後の画面にリダイレクト
chrome.runtime.onMessage.addListener(function (req, sender) {
window.open(req.href);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment