Created
December 15, 2009 09:55
-
-
Save anekos/256821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @name Session Manager | |
// @description for "Session Manager" addon | |
// @description-ja for "Session Manager" addon | |
// @license Creative Commons 2.1 (Attribution + Share Alike) | |
// @version 1.0 | |
// @author anekos (anekos@snca.net) | |
// @maxVersion 2.0pre | |
// @minVersion 2.0pre | |
// ==/VimperatorPlugin== | |
// | |
// Usage: | |
// | |
// Usage-ja: | |
// | |
// Links: | |
// https://addons.mozilla.org/firefox/addon/2324 | |
(function () { | |
const smID = '{1280606b-2510-4fe0-97ef-9b5a22eafe30}'; | |
const sm = window.gSessionManager; | |
const newFilenameBase = 'new-session-'; | |
if (!Application.extensions.has(smID) || !Application.extensions.get(smID).enabled || !sm){ | |
Components.utils.reportError('Vimperator: SessionManager is not installed'); | |
return; | |
} | |
function fixFileName (str) | |
str.replace(/[:|]/g, '-').replace(/[\\\/;*?"<>|]/g, "_").substr(0, 64); | |
function getNewFilename () { | |
let ns = [parseInt(it.fileName.match(/\d+/), 10) | |
for each (it in sm.getSessions()) | |
if (it.fileName && it.fileName.match("^" + newFilenameBase + "\d+$"))].sort(function (a, b) (a - b)); | |
let newNum = (function (){ | |
for (let i = 0; i < ns.length; i++) | |
if (ns[i] != i) | |
return i; | |
return ns.length; | |
})(); | |
return newFilenameBase + newNum; | |
} | |
function has (str, sub) | |
str.toLowerCase().indexOf(sub.toLowerCase()); | |
function getSessions (word) { | |
let f = word ? function (session) has(session.fileName, word) && has(session.name, word) | |
: function () true; | |
let result = []; | |
sm.getSessions().forEach(function (session) { | |
if (session.fileName && session.name && f(session)) | |
result.push([session.fileName, session.name]); | |
}); | |
return result; | |
} | |
function completer (context, args) { | |
context.title = ['Filename', 'Name']; | |
context.completions = getSessions(context.filter); | |
} | |
function convertModeString (mode) { | |
switch (mode.toLowerCase()) { | |
case 'r': return 'overwrite'; | |
case 'a': return 'append'; | |
} | |
return 'replace'; | |
} | |
commands.addUserCommand( | |
['smark'], | |
'saves current session', | |
function (args) { | |
let title = args.string || getNewFilename(); | |
sm.save(args['-k'] || title, fixFileName(title), false); | |
}, | |
{ | |
argCount: '*', | |
literal: true, | |
options: [ | |
[['-k', '-keyword'], commands.OPTION_STRING], | |
[['-w'], commands.OPTION_BOOL], | |
], | |
completer: function (context, args) { | |
completer.apply(this, arguments); | |
context.completions.unshift([new Date().toLocaleString(), '']); | |
context.completions.unshift([buffer.title, '']); | |
} | |
}, | |
true | |
); | |
commands.addUserCommand( | |
['sopen'], | |
'opens identified session(s)', | |
function (args) { | |
sm.load(args.string, convertModeString(args['-m'] || 'r')); | |
}, | |
{ | |
argCount: '1', | |
literal: true, | |
options: [ | |
[['-m', '-mode'], commands.OPTION_STRING], | |
], | |
completer: completer | |
}, | |
true | |
); | |
commands.addUserCommand( | |
['sdel'], | |
'deletes identified session(s)', | |
function (args) { | |
sm.remove(args.string); | |
}, | |
{ | |
argCount: '1', | |
literal: true, | |
options: [ | |
[['-m', '-mode'], commands.OPTION_STRING], | |
], | |
completer: completer | |
}, | |
true | |
); | |
commands.addUserCommand( | |
['smarks'], | |
'lists saved sessions', | |
function (args) { | |
liberator.echo(getSessions(args.string || '').map(function (it) it.join(': ')).join('\n')); | |
}, | |
{ | |
completer: completer | |
}, | |
true | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment