Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name hide_google_left_navi
// @namespace http://shiba-yu36.net/
// @description googleの左サイドバーのナビゲーションを表示したり、非表示にしたりするボタンを作成
// @include http://www.google.com/search?*
// @include http://www.google.com/images?*
// @include http://www.google.co.jp/search?*
// @include http://www.google.co.jp/images?*
// ==/UserScript==
(function(){
var margin_left_in_close = "30px";//閉じたときのleft margin
var margin_left_in_open = document.getElementById("center_col").style.marginLeft;//開いたときのleft margin
var default_centercol_texts_width = document.evaluate( /*それぞれの検索結果のテキストのデフォルトの長さ*/
'//*[@id="ires"]/ol/li/div',
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
).snapshotItem(0).style.maxWidth;
//左サイドバーを閉じる関数
function closeLeftNav(){
var leftnav = document.getElementById("leftnav");
var centercol = document.getElementById("center_col");
var open_button = document.getElementById("open-button");
leftnav.style.display = 'none';
centercol.style.marginLeft = margin_left_in_close;
open_button.style.display = '';
var centercol_texts = document.evaluate('//*[@id="ires"]/ol/li/div', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for ( var i=0 ; i < centercol_texts.snapshotLength; i++ ) {
centercol_texts.snapshotItem(i).style.maxWidth = "50em";
}
}
//左サイドバーを開く関数
function openLeftNav(){
var leftnav = document.getElementById("leftnav");
var centercol = document.getElementById("center_col");
var open_button = document.getElementById("open-button");
leftnav.style.display = '';
centercol.style.marginLeft = margin_left_in_open;
open_button.style.display = 'none';
var centercol_texts = document.evaluate('//*[@id="ires"]/ol/li/div', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for ( var i=0 ; i < centercol_texts.snapshotLength; i++ ) {
centercol_texts.snapshotItem(i).style.maxWidth = default_centercol_texts_width;
}
}
//閉じるボタンの配置
function addCloseButton(){
var leftnav = document.getElementById("leftnav");
var div = document.createElement('div');
div.style.width = leftnav.style.width;
div.style.marginBottom = "20px";
var img = document.createElement('img');
img.src = "http://img.f.hatena.ne.jp/images/fotolife/s/shiba_yu36/20100610/20100610203411.png?1276169702";
img.width = 20;
img.height = 20;
img.align = "right";
div.appendChild(img);
leftnav.insertBefore(div, leftnav.firstChild);
img.addEventListener('click', closeLeftNav, false);
}
//開くボタンの配置
function addOpenButton(){
var centercol = document.getElementById("center_col");
var div = document.createElement('div');
div.id = "open-button";
div.style.width = margin_left_in_close;
div.style.position = "absolute";
div.style.top = "2px";
div.style.left = "-2px";
var img = document.createElement('img');
img.src = "http://img.f.hatena.ne.jp/images/fotolife/s/shiba_yu36/20100610/20100610203412.png";
img.width = 20;
img.height = 20;
img.align = "right";
div.appendChild(img);
centercol.parentNode.insertBefore(div, centercol);
img.addEventListener('click', openLeftNav, false);
}
addOpenButton();
addCloseButton();
closeLeftNav();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment