Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name OperaActions.uc.js
// @namespace http://d.hatena.ne.jp/Griever/
// @include main
// @note keyconfigなどで呼び出して使う予定
// @version 10/28 fastForward の「次へ」のチェックを正規表現から前方一致に変更。
// ==/UserScript==
// @version 10/10 wand を追加。それに伴い fastForward を強化
if (typeof opera == 'undefined') var opera = { toString : function(){ return "[object OperaActions]" } };
opera.goToParentDirectory = function(){
// http://www.xuldev.org/blog/?p=76
var uri = gBrowser.currentURI;
if (uri.path == "/")
return;
var pathList = uri.path.split("/");
if (!pathList.pop())
pathList.pop();
loadURI(uri.prePath + pathList.join("/") + "/");
};
opera.fastForward = function(win){
if (!win) var win = document.commandDispatcher.focusedWindow;
if (win == window) win = content;
if (opera.wand(win)) return true;
var doc = win.document;
var locationHref = win.location.href;
var isPrev = ['d.hatena.ne.jp', 'm.twitter.com', 'www.xuldev.org'].indexOf(doc.domain) >= 0;
var nextLink = isPrev? '/descendant::link[@rel="prev"]' : '/descendant::link[@rel="next"]';
if (window.AutoPagerizeWedataSiteinfo){
window.AutoPagerizeWedataSiteinfo.some(function(info){
try {
if (new RegExp(info.url).test(locationHref)){
nextLink = info.nextLink;
return true;
}
}catch(e){}
});
}
var next = doc.evaluate(nextLink, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (next){
win.location.href = next.href;
return true;
}
var arr = isPrev?
['\u524D','\u623B\u308B','prev','<<','\xAB']:
['\u6B21','\u7D9A\u304D','\u9032\u3080','next','\u3082\u3063\u3068\u8AAD\u3080','>>','\xBB','\uFF1E'];
// 前方一致。アルファベットは小文字で書く(大文字小文字区別せず探す)
// var arr = isPrev?
// ['前','戻る','prev','<<','«']:
// ['次','\n次','続き','進む','next','もっと読む','>>','»','>'];
// テキストノードを小文字に変換し、改行、タブ文字、半角スペースを削除
var before = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\t ';
var after = 'abcdefghijklmnopqrstuvwxyz';
nextLink = arr.map(function(str){
if (str.indexOf('"') >= 0) return '';
return '//text()[starts-with(translate( self::text(), "'+ before +'", "'+ after +'"),"' + str + '")]/ancestor-or-self::a'
+'|//img[starts-with(translate( @alt, "'+ before +'", "'+ after +'"),"' + str + '")]/ancestor-or-self::a';
}).join('|');
var x = doc.evaluate(nextLink, doc, null, 7, null);
if (x.snapshotLength){
next = x.snapshotItem(x.snapshotLength-1);
next.style.outline = 'red 2px solid';
next.focus();
win.location.href = next.href;
return true;
}
if (/^(.*?)(\d+)($|\D+)$/.test(locationHref)){
var num = RegExp.$2, z = 0;
if (num != '0')
while(num[z] == '0') z++;
for (var i = 0, num = parseInt(num, 10) + 1; i < z; i++, num = '0' + num);
win.location.href = RegExp.$1 + num + RegExp.$3;
return true;
}
};
opera.pasteAndGo = function(){
var str = readFromClipboard();
if (str) loadURI(str);
};
opera.wand = function(win){
if (!win) win = document.commandDispatcher.focusedWindow;
if (win == window) win = content;
// パスワード入力欄があるか、見えているかの確認
var passwordField = win.document.querySelector('form input[type="password"]');
if (!passwordField || passwordField.offsetHeight == 0) return false;
var style = passwordField.ownerDocument.defaultView.getComputedStyle(passwordField, null);
if (style.visibility == 'hidden' || style.opacity == '0') return false;
var form = passwordField.form;
var passwordManager = Cc['@mozilla.org/login-manager;1'].getService(Ci.nsILoginManager);
var logins = passwordManager.findLogins(
{},
win.location.href.match(/.*?\:\/\/[^/]+/),
form.action.match(/.*?\:\/\/[^/]+/),
null);
if (!logins.length) return false;
var usernameField = form.querySelector('input[name="' + logins[0].usernameField + '"]');
usernameField.value = logins[0].username;
passwordField.value = logins[0].password;
form.submit();
return true;
};
opera.toggleSidebar = function(commandID, forceOpen){
var sidebarBox = document.getElementById("sidebar-box");
var lastcommand = sidebarBox.getAttribute("sidebarcommand") || sidebarBox.getAttribute('sidebarlastcommand') || 'viewBookmarksSidebar';
sidebarBox.setAttribute('sidebarlastcommand', lastcommand);
if (!commandID) commandID = lastcommand;
toggleSidebar(commandID, forceOpen);
};
opera.spaceKey = function(){
var win = document.commandDispatcher.focusedWindow;
if (win != window && (win.scrollMaxY - win.scrollY <= 1) ){
opera.fastForward(win);
}else{
goDoCommand("cmd_scrollPageDown");
};
};
opera.gestureRight = function(){
var webNav = getWebNavigation();
var sessionHistory = webNav.sessionHistory;
if (sessionHistory.count - sessionHistory.index == 1){
opera.fastForward();
}else{
gBrowser.goForward();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment