randomfox (randomfox) wrote,
randomfox
randomfox

K-Meleon web search macros

In toolbars.cfg:
Main Bar {
.
.
.
        Search{
        luamacros(GoogleSearch)|Web Search
        Search
        toolhot.bmp[5]
        }
.
.
.
}
In menus.cfg:
Web Search{
luamacros(GoogleSearch, Search Google)
luamacros(A9Search, Search A9)
luamacros(DictSearch, Search Dictionary.com)
luamacros(BaiduSearch, Search Baidu)
luamacros(MWSearch, Search Merriam-Webster)
luamacros(WikiSearch, Search Wikipedia)
luamacros(IMDBSearch, Search IMDB)
luamacros(BlogSearch, Search Blog)
}
In accel.cfg:
CTRL G = luamacros(GoogleSearch)
Lua code:
function encode(url)
	return string.gsub(url, "([^a-zA-Z0-9_%.%-])", function (s)
		if s == " " then
			return "+"
		else
			return string.format("%%%02X", string.byte(s))
		end
	end)
end

local function Trim(str)
    str = string.gsub(str, "%s+$", "")
    str = string.gsub(str, "^%s+", "")
    return str
end

local lastsearch = ""

local function GetSearchWord(engineName)
    local clip = getclipboard()
    setclipboard("")
    id("ID_EDIT_COPY")
    local word = getclipboard()
    setclipboard(clip)
    if word and word ~= "" and word ~= lastsearch then
	lastsearch = Trim(word)
	return lastsearch
    end
    lastsearch = ""
    return prompt("Search", engineName)
end

local function GenSearch(engine, name)
    return function()
	local word
--        alert(engine .. name, "RunSearch", ICON_INFO)
	word = GetSearchWord(name)
	if word and word ~= "" then
	    word = encode(EncodeUTF8(word))
	    NavigateTo(engine .. word, OPEN_NEW)
	end
    end
end

GoogleSearch = GenSearch("http://www.google.com/search?q=", "Google")
A9Search = GenSearch("http://a9.com/", "A9")
BaiduSearch = GenSearch("http://www.baidu.com/s?wd=", "Baidu")
MWSearch = GenSearch("http://www.m-w.com/dictionary/", "Merriam-Webster")
DictSearch = GenSearch("http://dictionary.reference.com/search?q=", "Dictionary.com")
WikiSearch = GenSearch("http://en.wikipedia.org/wiki/", "Wikipedia")
IMDBSearch = GenSearch("http://www.imdb.com/find?q=", "IMDB")
BlogSearch = GenSearch("http://blogsearch.google.com/blogsearch?hl=en&q=", "GoogleBlog")

-- The End

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments