Skip to content

Instantly share code, notes, and snippets.

@azu
Forked from gifnksm/nicovideo_createPanel.js
Created February 27, 2011 05:24
Show Gist options
  • Save azu/845920 to your computer and use it in GitHub Desktop.
Save azu/845920 to your computer and use it in GitHub Desktop.
ニコニコ動画のwatchページのタブを追加する関数(Firefox依存なし)
// http://blog.livedoor.jp/eeu/archives/55316335.html
var addClassName = function(el, className) {
var reg = new RegExp('(^| +)' + className + '($| +)');
if (!reg.test(el.className))
el.className += ' ' + className;
};
var removeClassName = function(el, className) {
var reg = new RegExp('(^| +)' + className + '($| +)');
el.className = el.className.replace(reg, ' ');
};
function nicovideo_createPanel(id, labelText) {
var panelID = 'itab_' + id;
var label = document.createElement('a');
label.href = '#' + panelID;
label.innerHTML = '<div>' + labelText + '</div>';
// remove extra <a>...</a>
label.firstChild.textContent = labelText;
document.querySelector('#itab td').appendChild(label);
var panel = document.createElement('div');
panel.id = panelID;
panel.className = 'info';
document.querySelector('.info_frm').appendChild(panel);
//unsafeWindow.cont = unsafeWindow.$$('.info_frm .info');
var act = "in";// unsafeWindow.act
label.addEventListener('click', function(e) {
e.preventDefault();
Array.forEach(document.querySelectorAll('#itab td a'), function(elm) {
removeClassName(elm, act)
});
addClassName(label, act);
Array.forEach(document.querySelectorAll('.info_frm .info'), function(elm) {
elm.id === panelID
? addClassName(elm, act)
: removeClassName(elm, act)
});
}, false);
return { label: label, panel: panel };
}
// ニコニコ大百科という名前のタブを作る (panel.id == nicopedia)
var nicovideo_createPanel_res = nicovideo_createPanel('nicopedia', 'ニコニコ大百科'),
panel = nicovideo_createPanel_res.panel,
label = nicovideo_createPanel_res.label;
// タブのパネルに表示する内容を指定する
panel.appendChild(document.createTextNode('hogehoge'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment