Skip to content

Instantly share code, notes, and snippets.

@hluk
Last active August 20, 2019 05:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluk/b598ab08e00ce9618b33 to your computer and use it in GitHub Desktop.
Save hluk/b598ab08e00ce9618b33 to your computer and use it in GitHub Desktop.
CopyQ - Tab for URLs with Title and Icon
[Command]
Automatic=true
Command="
copyq:
var tabName = '&url'
function lower(data) {
return str(data).toLowerCase()
}
function isHtml(reply) {
var headers = reply.headers
for (var i in headers) {
var header = headers[i]
if (lower(header[0]) === \"content-type\")
return lower(header[1]).indexOf(\"text/html\") === 0
}
return false
}
function curl(url) {
reply = networkGet(url)
if (isHtml(reply))
return reply.data
return \"\"
}
function grep(content, re) {
return content ? (re.exec(content) || [])[1] : ''
}
function getTitle(content) {
return grep(content, /<title>([^<]*)<\\/title>/i)
}
function getFavicon(content) {
var icon =
grep(
grep(content, /<link([^>]*rel=[\"'](?:shortcut )?icon[\"'][^>]*)/i),
/href=[\"']([^\"']*)/i
)
if (icon && icon[0] === '/') {
if (icon[1] === '/') {
var i = url.search(/\\/\\//)
var protocol = i == -1 ? 'http:' : url.substr(0, i)
icon = 'http:' + icon
} else {
var baseUrl = url.substr(0, url.search(/[^\\/:](\\/|$)/) + 1)
icon = baseUrl + icon
}
}
return icon
}
tab(tabName)
var url = str(input()).replace(/\\s.*/, '')
if (url === str(read(0)))
abort()
var content = str(curl(url))
var title = getTitle(content)
var icon = getFavicon(content)
write(
'text/plain', url,
'application/x-copyq-item-notes', title || '',
'application/x-copyq-item-icon', icon ? curl(icon) : ''
)"
Icon=\xf0c1
Input=text/plain
Match=^https?://
Name=Tab for URLs with Title and Icon
@nsomar
Copy link

nsomar commented Apr 25, 2016

This looks super cool.
How do I add this to copyq?

@ousui
Copy link

ousui commented Jul 26, 2016

@oarrabi
copy this and open copyq main windows, then press F6, and ctrl+v

@pihentagy
Copy link

@hluk Works as expected, but icons are not shown. (using 2.8.0)

@royharoush
Copy link

in version Clipboard Manager testing-10-g45c47562.
doesn't work that well, on and off sort of speak.

@hluk
Copy link
Author

hluk commented Mar 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment