forked from ywzhaiqi/userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchHelper.user.js
67 lines (57 loc) · 1.75 KB
/
searchHelper.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// ==UserScript==
// @name Search Hepler
// @namespace https://github.com/ywzhaiqi
// @author ywzhaiqi
// @version 0.2
// @description 让一些特殊的搜索支持搜索串
// @include http://kindleren.com/search.php?mod=forum&mq=*
// @include http://shuzi.taobao.com/item/search---50094067.htm?q=*
// @grant GM_addStyle
// @require http://cdn.staticfile.org/zepto/1.1.4/zepto.min.js
// @run-at document-start
// @noframes
// ==/UserScript==
if (typeof String.prototype.contains != 'function') {
String.prototype.contains = function(str) {
return this.indexOf(str) != -1;
};
}
function getParam(name, url) {
var regexp = new RegExp("(?:^|\\?|#|&)" + name + "=([^&#]*)(?:$|&|#)", "i"),
matches = regexp.exec(url || location.href);
return matches ? decodeURIComponent(matches[1]) : ""
}
function goSearch(param, input, submit) {
$(function() {
var search = getParam(param, location.href);
if (search) {
$(input).val(search);
$(submit).click();
}
});
}
// 运行在 document-start,详见 taobao 函数
function goSearch2(url, param) {
var search = getParam(param, location.href);
if (search) {
location.href = url.replace('%s', search);
}
}
var ns = {
kindleren: function() {
goSearch('mq', '#scform_srchtxt', '#scform_submit');
},
taobao: function() { // 解决乱码问题
goSearch2('http://shuzi.taobao.com/item/search-.htm?q=%s&isbook=ebook', 'q');
}
};
function run() { // 自动运行符合 host 的函数
var host = location.host;
Object.keys(ns).some(function(key) {
if (host.contains(key)) {
ns[key]();
return true;
}
});
}
run();