Created
June 28, 2014 20:07
-
-
Save pckujawa/28c9ffb181436f6fd022 to your computer and use it in GitHub Desktop.
Twitter bookmarklet that actually works
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
//javascript: | |
(function() { | |
window.twttr = window.twttr || {}; | |
var D = 550, | |
A = 450, | |
C = screen.height, | |
B = screen.width, | |
H = Math.round((B / 2) - (D / 2)), | |
G = 0, | |
F = document, | |
E; | |
if (C > A) { | |
G = Math.round((C / 2) - (A / 2)) | |
} | |
window.twttr.shareWin = window.open('//twitter.com/share', '', 'left=' + H + ',top=' + G + ',width=' + D + ',height=' + A + ',personalbar=0,toolbar=0,scrollbars=1,resizable=1'); | |
}()); | |
(function() { | |
var F, A, D; | |
function B(M) { | |
var N = [], | |
L = 0, | |
K = M.length; | |
for (; L < K; L++) { | |
N.push(M[L]) | |
} | |
return N | |
} | |
function H(K) { | |
return encodeURIComponent(K).replace(/\+/g, "%2B") | |
} | |
function C(M) { | |
var L = [], | |
K; | |
for (K in M) { | |
if (M[K] !== null && typeof M[K] !== "undefined") { | |
L.push(H(K) + "=" + H(M[K])) | |
} | |
} | |
return L.sort().join("&") | |
} | |
function I() { | |
var L = document.getElementsByTagName("a"), | |
R = document.getElementsByTagName("link"), | |
K = /\bme\b/, | |
N = /^https?\:\/\/(www\.)?twitter.com\/([a-zA-Z0-9_]+)$/, | |
Q = B(L).concat(B(R)), | |
P, T, M, O = 0, | |
S; | |
for (; | |
(S = Q[O]); O++) { | |
T = S.getAttribute("rel"); | |
M = S.getAttribute("href"); | |
if (T && M && T.match(K) && (P = M.match(N))) { | |
return P[2] | |
} | |
} | |
} | |
function G() { | |
var K = window.getSelection && String(window.getSelection()); | |
return K && "%C3%A2%E2%82%AC%C5%93" + K.replace(/\n/, " ").replace(/(^\s+)|(\s+$)/, "") + "%C3%A2%E2%82%AC%C2%9D" | |
} | |
function E(L) { | |
var K; | |
if (L.match(/^https?:\/\//)) { | |
return L | |
} else { | |
K = location.host; | |
if (location.port.length > 0) { | |
K += ":" + location.port | |
} | |
if (L.match(/^\/[^\/]/)) { | |
return [location.protocol, "//", K, L].join("") | |
} else { | |
return [location.protocol, "//", K, location.pathname.replace(/\/([^\/]+)$/, "/"), L].join("") | |
} | |
} | |
} | |
function J() { | |
var K = document.getElementsByTagName("link"), | |
L, M; | |
for (L = 0, M; | |
(M = K[L]); L++) { | |
if (M.getAttribute("rel") == "canonical") { | |
return E(M.getAttribute("href")) | |
} | |
} | |
return null | |
} | |
D = (function() { | |
var L = { | |
text: G() || decodeURIComponent(document.title), | |
url: (J() || location.href), | |
_: ((new Date()).getTime()) | |
}, K = I(); | |
if (K) { | |
L.via = K | |
} | |
return C(L) | |
}()); | |
F = window.twttr.shareWin; | |
A = "//twitter.com/share?" + D; | |
console.log(A); | |
if (null === F) { | |
window.location.href = A | |
} else { | |
if (F && F.open) { | |
F.location.href = A; | |
F.focus() | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment