Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save huangenguo/dd8aea39f321bc8ebf29644036d4bd50 to your computer and use it in GitHub Desktop.
Save huangenguo/dd8aea39f321bc8ebf29644036d4bd50 to your computer and use it in GitHub Desktop.
Surfingkeys customization with jd/douban/taobao search and copy tab(s) as markdown feature.
// 参考资料
// Surfingkeys 实用向推荐 - 少数派 -> https://sspai.com/post/63692
// 引用链接生成器 -> https://greasyfork.org/zh-CN/scripts/11800-reflinkgenerator
// 链接地址洗白白 -> https://greasyfork.org/zh-CN/scripts/373270-%E9%93%BE%E6%8E%A5%E5%9C%B0%E5%9D%80%E6%B4%97%E7%99%BD%E7%99%BD
// Easy Search Help. -> https://s.appinn.me/help.html
// 3.7k★ brookhong/Surfingkeys: Map your keys for web surfing, expand your browser with javascript and keyboard. -> https://github.com/brookhong/Surfingkeys
// an example to replace `T` with `gt`, click `Default mappings` to see how `T` works.
// map('gt', 'T')
// map('<Ctrl-->', 'B')
// an example to remove mapkey `Ctrl-i` 没有效果
// unmap('<Ctrl-f>')
// 提示词显示在超链接的右侧
// settings.hintAlign = "right";
// custormization start 自定义开始
removeSearchAliasX('d', 's')
// 添加搜索 京东搜索 豆瓣图书 淘宝搜索
addSearchAliasX(
'jd',
'京东',
'http://search.jd.com/Search?enc=utf-8&keyword=',
's',
)
addSearchAliasX(
'dbb',
'豆瓣图书',
'https://search.douban.com/book/subject_search?cat=1001&search_text=',
// 'https://www.douban.com/search?cat=1001&q=',
's',
)
addSearchAliasX(
'tb',
'淘宝',
'https://s.taobao.com/search?q=',
's',
'https://suggest.taobao.com/sug?code=utf-8&q=',
function (response) {
var res = []
try {
res = JSON.parse(response.text).result.map(function (it) {
return it[0]
})
} catch (e) {}
return res
},
)
// 净化链接 修改微信公众号文章title标题;豆瓣去掉;GitHub 带上 star 数;知乎去掉「X 条消息 - 知乎」
// 移除 URL 的 query 和 hash 部分
function _cleanupUrl(url, doCleanup) {
return doCleanup ? /http[^#\?]*/.exec(url)[0] : url
}
//标签页标题和链接的MarkDown格式或PlainText格式
function _getCurrentTab(doCleanup,doPlainText) {
let title = document.title
const url = location.href
const urlHost = location.hostname
const urlPath = location.pathname
let prefix = ''
// get actual article title for Wechat Public Account page.
// comment out if you already have a userscript rectifying the title.
// e.g. [修改微信公众号文章title标题](https://greasyfork.org/en/scripts/375439-%E4%BF%AE%E6%94%B9%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0title%E6%A0%87%E9%A2%98)
if (urlHost === 'mp.weixin.qq.com') {
title = document.getElementById('activity-name').innerText
}
// Alfred Forum 页的标题精简
// if (urlHost.includes('www.alfredforum.com')) {
// title = title.replace('- Discussion & Help - Alfred App Community Forum', '')
// }
// Zhihu 页的标题精简掉私信、消息提示文字
if (urlHost.includes('www.zhihu.com')) {
title = title.replace(/\(\d+.*(消息|私信)\) /, '').replace(' - 知乎', '')
}
// GitHub 页标题精简
if (urlHost === 'github.com') {
const issuesRegexPattern = /issues\/\d+$/
if (issuesRegexPattern.test(urlPath)) {
const issuePathParts = /\/(.*)\/issues\/(\d+)$/.exec(urlPath)
// Github issue 页的标题精简为 ${repo-name}#${issue-no},如 Webpack#123
const [, repoName, issueNo] = issuePathParts
title = `${repoName}#${issueNo}`
} else {
// assume we are at repo root, or monorepo subproject root
const starCountEl = document.querySelector('#repo-stars-counter-star')
const starCount = (starCountEl && starCountEl.innerText.trim()) || '0'
prefix =
starCount.endsWith('k') || parseInt(starCount) > 99
? `${starCount}★ `
: ''
}
}
if (doPlainText) {
return `${prefix}${title} -> ${_cleanupUrl(url, doCleanup)}`
} else {
return `${prefix}[${title}](${_cleanupUrl(url, doCleanup)})`
}
}
function _copyAllTabs(doCleanup,doPlainText) {
RUNTIME('getTabs', null, (response) => {
const tabs = (response && response.tabs) || []
markdownStr = tabs
.filter(
(tab) => tab.url.startsWith('http') && tab.url.indexOf('cache') === -1,
)
.map((tab) => `[${tab.title}](${_cleanupUrl(tab.url, doCleanup)})`)
.concat(_getCurrentTab(doCleanup,doPlainText))
.join('\n')
Clipboard.write(markdownStr)
})
}
mapkey(
'ybp',
// 'Copy current tab in PlainText format, removing query and hash in URL',
'纯文本格式复制当前标签页',
function () {
Clipboard.write(_getCurrentTab(false,1))//true为净化链接,1为纯文本格式
},
{},
)
mapkey(
'ybcp',
// 'Copy current tab in PlainText format, removing query and hash in URL',
'净化链接,纯文本格式复制当前标签页',
function () {
Clipboard.write(_getCurrentTab(true,1))//true为净化链接,1为纯文本格式
},
{},
)
mapkey(
'ybap',
// 'Copy all tabs in Markdown format, removing query and hash in URL',
'纯文本格式复制所有标签页',
function () {
_copyAllTabs(false,1)
},
{},
)
mapkey(
'ybcap',
// 'Copy all tabs in Markdown format, removing query and hash in URL',
'净化链接,纯文本格式复制所有标签页',
function () {
_copyAllTabs(true,1)
},
{},
)
mapkey(
'ybm',
// 'Copy current tab in Markdown format',
'Markdown 格式复制当前标签页',
function () {
Clipboard.write(_getCurrentTab(false,0))
},
{},
)
mapkey(
'ybcm',
// 'Copy current tab in Markdown format, removing query and hash in URL',
'净化链接,Markdown 格式复制当前标签页',
function () {
Clipboard.write(_getCurrentTab(true,0))
},
{},
)
mapkey(
'ybam',
// 'Copy all tabs in Markdown format',
'Markdown 格式复制所有标签页',
function () {
_copyAllTabs(false,0)
},
{},
)
mapkey(
'ybcam',
// 'Copy all tabs in Markdown format, removing query and hash in URL',
'净化链接,Markdown 格式复制所有标签页',
function () {
_copyAllTabs(true,0)
},
{},
)
// customization end
// set theme 设置主题
settings.theme = `
.sk_theme {
font-family: Input Sans Condensed, Charcoal, sans-serif;
font-size: 10pt;
background: #24272e;
color: #abb2bf;
}
.sk_theme tbody {
color: #fff;
}
.sk_theme input {
color: #d0d0d0;
}
.sk_theme .url {
color: #61afef;
}
.sk_theme .annotation {
color: #56b6c2;
}
.sk_theme .omnibar_highlight {
color: #528bff;
}
.sk_theme .omnibar_timestamp {
color: #e5c07b;
}
.sk_theme .omnibar_visitcount {
color: #98c379;
}
.sk_theme #sk_omnibarSearchResult>ul>li:nth-child(odd) {
background: #303030;
}
.sk_theme #sk_omnibarSearchResult>ul>li.focused {
background: #3e4452;
}
#sk_status, #sk_find {
font-size: 20pt;
}`
// click `Save` button to make above settings to take effect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment