/
nicopedia-sp-previewer.user.js
112 lines (100 loc) · 3.52 KB
/
nicopedia-sp-previewer.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ==UserScript==
// @name Nicopedia-Smartphone-Previewer
// @namespace https://github.com/kosh04/userscript
// @version 0.2.20190308
// @description ニコニコ大百科の記事編集ページにスマートフォン版のプレビューを追加する
// @grant GM_addStyle
// @match https://dic.nicovideo.jp/p/a/*
// @match https://dic.nicovideo.jp/p/v/*
// @match https://dic.nicovideo.jp/p/i/*
// @match https://dic.nicovideo.jp/p/l/*
// @match https://dic.nicovideo.jp/p/u/*
// @match https://dic.nicovideo.jp/p/c/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @author kosh (mono)
// @license Public domain
// @noframes
// ==/UserScript==
// ChangeLog
// =========
// * 0.2.20190308 PC版サイトのデザインリニューアルに伴うCSSセレクタの修正
/* global jQuery, $ */
(function($) {
"use strict";
const $article = $('#nicopedia-article-textarea');
if ($article.length === 0) {
return;
}
GM_addStyle(`
.sp-window { padding: 5px; }
.sp-window h2 { font-size: 1.3em; border-bottom: double 1px #8cc700; margin: 0.4em 0; }
.resizable { overflow: auto; resize: both; }
.sp-view { background-color: #fff; border: solid 1px #66a32f; margin: 10px auto; }
.sp-view iframe { overflow: hidden; width: 100%; height: 100%; }
#sp-view { width: 375px; height: 667px; }
`);
// デバイスサイズは以下のリンク参照
// https://material.io/devices/
// https://developer.apple.com/library/content/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html
const html = `
<div class="left-box" style="margin: 5px 0; background-image: none; border: solid 1px #aaa; border-radius: 5px;">
<div class="sp-window">
<h2>スマホ版プレビュー</h2>
<p>※実際の表示とは異なる場合があります</p>
<button class="sp-rotate-button" type="button">画面の回転</button>
<select id="sp-device-list">
<option data-width="320" data-height="480">320x480 (iPhone 4S)</option>
<option data-width="375" data-height="667" selected>375x667 (iPhone 6/7/8)</option>
<option data-width="600" data-height="960">600x960 (Nexus 7)</option>
<option data-width="768" data-height="1024">768x1024 (iPad)</option>
</select>
<div id="sp-view" class="sp-view">
<iframe srcdoc=""></iframe>
</div>
</div>
</div>
`;
const srcdoc = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/css/reset.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div id="top" class="bs-Wrapper">
<div class="bs-Wrapper_Inner">
<section class="sw-Article">
<div class="sw-Article_TitleBox"><!-- 記事名 --></div>
<div class="sw-Article_Menu"><!-- ほめる/共有/その他 --></div>
<div class="sw-Article_Body">
<div class="sw-Article_Body-inner">
<span class="article">${$article.text()}</span>
</div>
</div>
</section>
</div>
</div>
</body>
</html>
`;
$(".article").before(html);
// $("#sp-view").addClass("resizable");
$("#sp-view > iframe").attr("srcdoc", srcdoc);
const $view = $("#sp-view");
$(".sp-rotate-button").click(() => {
$view.css({
width: $view.css("height"),
height: $view.css("width")
});
});
$("#sp-device-list").change((e) => {
const {width, height} = e.target[e.target.selectedIndex].dataset;
$view.css({
width: `${width}px`,
height: `${height}px`
});
});
}(jQuery));