LoginSignup
12

More than 3 years have passed since last update.

ツイートボタンがないページでもツイート画面を開くブックマークレット

Last updated at Posted at 2017-03-25

今見ているウェブページを共有するためのツイート画面を開くブックマークレットをつくりました。
ブックマークレットの使用方法については、使用している端末やブラウザによって異なるので、各自でググってください。
各ブックマークレットのツイート内容は、<タイトル> <URL>です。

ツイートダイアログを開く

ブックマークレット
javascript:(function(){var w=550,h=420;window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(document.title)+" "+encodeURIComponent(location.href),"_blank","width="+w+",height="+h+",left="+(screen.width-w)/2+",top="+(screen.height-h)/2+",scrollbars=yes,resizable=yes,toolbar=no,location=yes")})()

ブックマークレットを実行すると、ツイートダイアログを開きます。
ツイートボタンとほぼ同じ動作です。

新規タブでツイート画面を開く

ブックマークレット
javascript:window.open("https://twitter.com/intent/tweet?text="+encodeURIComponent(document.title)+" "+encodeURIComponent(location.href))

ブックマークレットを実行すると、新規タブでツイート画面を開きます。

Twitterアプリのツイート画面を開く

ブックマークレット
javascript:location.href="twitter://post?message="+encodeURIComponent(document.title)+" "+encodeURIComponent(location.href)

スマートフォン(iPhone、Android)でブックマークレットを実行すると、Twitterアプリのツイート画面を開きます。
URLスキームという仕組みで、通常はhttphttpsなどのプロトコルを指定するスキーム部に、twitterのようなアプリ固有のスキームを指定することで、そのアプリを開くことができます。
TwitterのURLスキームには複数のオプションがあり、今回はツイート画面を開くpostを使用しています。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12