1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Shortcuts]DataURLを使ってJSを実行する

Last updated at Posted at 2021-01-10
  1. HTMLのscriptタグに処理を書く
  2. HTMLを[URLエンコード]でエンコードする
  3. data:text/html,(ここにエンコードしたHTML)の形式でDataURLを作る
  4. 出力させたい値をdocument.write()などでBodyタグに書き出す
  5. [Webページの内容を取得]の入力に渡す
  6. [テキストを取得]

実践

とりあえず[辞書]アクションをJSON.stringify()で整形してみる。

ショートカット側

shortcut

辞書アクションの中身

{
  "fizz": "bizz",
  "bool": false,
  "apple": {
    "hiragana": "りんご",
    "chineseChar": "林檎"
  },
  "array": [
    "hoge",
    "fuga",
    "puyo"
  ]
}

テキストアクション内のHTML

<!DOCTYPE HTML>
<html lang="ja">
	<head>
	<meta charset="utf-8">
	<title>pretty-json</title>
	<script>
		const json = [辞書];  // [辞書]アクションに置き換える
		const pretty = JSON.stringify(json, null, "&emsp;").replace(/\n/g, "<BR>");
		document.write(pretty)
	</script>
  </head>
</html>

ちょっとミソ

HTMLで使うJSON.stringify()

// これだと整形されて表示されない(HTMLはスペースや改行は反映されないため)
const bad = JSON.stringify(json, null, "\t");

// スペースや改行をHTML用に置き換える
const good = JSON.stringify(json, null, "&emsp;").replace(/\n/g, "<BR>");
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?