File size: 11,447 Bytes
271613e 3634354 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e 613424e 271613e |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
import Tool from '../DevTools/Tool'
import Settings from '../Settings/Settings'
import $ from 'licia/$'
import escape from 'licia/escape'
import isEmpty from 'licia/isEmpty'
import contain from 'licia/contain'
import unique from 'licia/unique'
import each from 'licia/each'
import sameOrigin from 'licia/sameOrigin'
import ajax from 'licia/ajax'
import MutationObserver from 'licia/MutationObserver'
import toArr from 'licia/toArr'
import concat from 'licia/concat'
import map from 'licia/map'
import { isErudaEl, classPrefix as c } from '../lib/util'
import evalCss from '../lib/evalCss'
import Storage from './Storage'
import Cookie from './Cookie'
import { setState, getState } from './util'
export default class Resources extends Tool {
constructor() {
super()
this._style = evalCss(require('./Resources.scss'))
this.name = 'resources'
this.displayName = 'リソース'
this._hideErudaSetting = false
this._observeElement = true
}
init($el, container) {
super.init($el)
this._container = container
this._initTpl()
this._localStorage = new Storage(
this._$localStorage,
container,
this,
'local'
)
this._sessionStorage = new Storage(
this._$sessionStorage,
container,
this,
'session'
)
this._cookie = new Cookie(this._$cookie, container)
this._bindEvent()
this._initObserver()
this._initCfg()
}
refresh() {
return this.refreshLocalStorage()
.refreshSessionStorage()
.refreshCookie()
.refreshScript()
.refreshStylesheet()
.refreshIframe()
.refreshImage()
}
destroy() {
super.destroy()
this._localStorage.destroy()
this._sessionStorage.destroy()
this._disableObserver()
evalCss.remove(this._style)
this._rmCfg()
}
refreshScript() {
let scriptData = []
$('script').each(function () {
const src = this.src
if (src !== '') scriptData.push(src)
})
scriptData = unique(scriptData)
const scriptState = getState('script', scriptData.length)
let scriptDataHtml = '<li>なし</li>'
if (!isEmpty(scriptData)) {
scriptDataHtml = map(scriptData, (script) => {
script = escape(script)
return `<li><a href="${script}" target="_blank" class="${c(
'js-link'
)}">${script}</a></li>`
}).join('')
}
const scriptHtml = `<h2 class="${c('title')}">
スクリプト
<div class="${c('btn refresh-script')}">
<span class="${c('icon-refresh')}"></span>
</div>
</h2>
<ul class="${c('link-list')}">
${scriptDataHtml}
</ul>`
const $script = this._$script
setState($script, scriptState)
$script.html(scriptHtml)
return this
}
refreshStylesheet() {
let stylesheetData = []
$('link').each(function () {
if (this.rel !== 'stylesheet') return
stylesheetData.push(this.href)
})
stylesheetData = unique(stylesheetData)
const stylesheetState = getState('stylesheet', stylesheetData.length)
let stylesheetDataHtml = '<li>なし</li>'
if (!isEmpty(stylesheetData)) {
stylesheetDataHtml = map(stylesheetData, (stylesheet) => {
stylesheet = escape(stylesheet)
return ` <li><a href="${stylesheet}" target="_blank" class="${c(
'css-link'
)}">${stylesheet}</a></li>`
}).join('')
}
const stylesheetHtml = `<h2 class="${c('title')}">
スタイルシート
<div class="${c('btn refresh-stylesheet')}">
<span class="${c('icon-refresh')}"></span>
</div>
</h2>
<ul class="${c('link-list')}">
${stylesheetDataHtml}
</ul>`
const $stylesheet = this._$stylesheet
setState($stylesheet, stylesheetState)
$stylesheet.html(stylesheetHtml)
return this
}
refreshIframe() {
let iframeData = []
$('iframe').each(function () {
const $this = $(this)
const src = $this.attr('src')
if (src) iframeData.push(src)
})
iframeData = unique(iframeData)
let iframeDataHtml = '<li>なし</li>'
if (!isEmpty(iframeData)) {
iframeDataHtml = map(iframeData, (iframe) => {
iframe = escape(iframe)
return `<li><a href="${iframe}" target="_blank" class="${c(
'iframe-link'
)}">${iframe}</a></li>`
}).join('')
}
const iframeHtml = `<h2 class="${c('title')}">
Iframe
<div class="${c('btn refresh-iframe')}">
<span class="${c('icon-refresh')}"></span>
</div>
</h2>
<ul class="${c('link-list')}">
${iframeDataHtml}
</ul>`
this._$iframe.html(iframeHtml)
return this
}
refreshLocalStorage() {
this._localStorage.refresh()
return this
}
refreshSessionStorage() {
this._sessionStorage.refresh()
return this
}
refreshCookie() {
this._cookie.refresh()
return this
}
refreshImage() {
let imageData = []
const performance = (this._performance =
window.webkitPerformance || window.performance)
if (performance && performance.getEntries) {
const entries = this._performance.getEntries()
entries.forEach((entry) => {
if (entry.initiatorType === 'img' || isImg(entry.name)) {
if (contain(entry.name, 'exclude=true')) {
return
}
imageData.push(entry.name)
}
})
} else {
$('img').each(function () {
const $this = $(this)
const src = $this.attr('src')
if ($this.data('exclude') === 'true') {
return
}
imageData.push(src)
})
}
imageData = unique(imageData)
imageData.sort()
const imageState = getState('image', imageData.length)
let imageDataHtml = '<li>なし</li>'
if (!isEmpty(imageData)) {
// prettier-ignore
imageDataHtml = map(imageData, (image) => {
return `<li class="${c('image')}">
<img src="${escape(image)}" data-exclude="true" class="${c('img-link')}"/>
</li>`
}).join('')
}
const imageHtml = `<h2 class="${c('title')}">
画像
<div class="${c('btn refresh-image')}">
<span class="${c('icon-refresh')}"></span>
</div>
</h2>
<ul class="${c('image-list')}">
${imageDataHtml}
</ul>`
const $image = this._$image
setState($image, imageState)
$image.html(imageHtml)
return this
}
show() {
super.show()
if (this._observeElement) this._enableObserver()
return this.refresh()
}
hide() {
this._disableObserver()
return super.hide()
}
_initTpl() {
const $el = this._$el
$el.html(
c(`<div class="section local-storage"></div>
<div class="section session-storage"></div>
<div class="section cookie"></div>
<div class="section script"></div>
<div class="section stylesheet"></div>
<div class="section iframe"></div>
<div class="section image"></div>`)
)
this._$localStorage = $el.find(c('.local-storage'))
this._$sessionStorage = $el.find(c('.session-storage'))
this._$cookie = $el.find(c('.cookie'))
this._$script = $el.find(c('.script'))
this._$stylesheet = $el.find(c('.stylesheet'))
this._$iframe = $el.find(c('.iframe'))
this._$image = $el.find(c('.image'))
}
_bindEvent() {
const $el = this._$el
const container = this._container
$el
.on('click', '.eruda-refresh-script', () => {
container.notify('更新しました', { icon: 'success' })
this.refreshScript()
})
.on('click', '.eruda-refresh-stylesheet', () => {
container.notify('更新しました', { icon: 'success' })
this.refreshStylesheet()
})
.on('click', '.eruda-refresh-iframe', () => {
container.notify('更新しました', { icon: 'success' })
this.refreshIframe()
})
.on('click', '.eruda-refresh-image', () => {
container.notify('更新しました', { icon: 'success' })
this.refreshImage()
})
.on('click', '.eruda-img-link', function () {
const src = $(this).attr('src')
showSources('img', src)
})
.on('click', '.eruda-css-link', linkFactory('css'))
.on('click', '.eruda-js-link', linkFactory('js'))
.on('click', '.eruda-iframe-link', linkFactory('iframe'))
function showSources(type, data) {
const sources = container.get('sources')
if (!sources) return
sources.set(type, data)
container.showTool('sources')
return true
}
function linkFactory(type) {
return function (e) {
if (!container.get('sources')) return
e.preventDefault()
const url = $(this).attr('href')
if (type === 'iframe' || !sameOrigin(location.href, url)) {
showSources('iframe', url)
} else {
ajax({
url,
success: (data) => {
showSources(type, data)
},
dataType: 'raw',
})
}
}
}
}
_rmCfg() {
const cfg = this.config
const settings = this._container.get('settings')
if (!settings) return
settings
.remove(cfg, 'hideErudaSetting')
.remove(cfg, 'observeElement')
.remove('Resources')
}
_initCfg() {
const cfg = (this.config = Settings.createCfg('resources', {
hideErudaSetting: true,
observeElement: true,
}))
if (cfg.get('hideErudaSetting')) this._hideErudaSetting = true
if (!cfg.get('observeElement')) this._observeElement = false
cfg.on('change', (key, val) => {
switch (key) {
case 'hideErudaSetting':
this._hideErudaSetting = val
return
case 'observeElement':
this._observeElement = val
return val ? this._enableObserver() : this._disableObserver()
}
})
const settings = this._container.get('settings')
settings
.text('リソース')
.switch(cfg, 'hideErudaSetting', 'Eruda設定を非表示にする')
.switch(cfg, 'observeElement', '自動リフレッシュ要素')
.separator()
}
_initObserver() {
this._observer = new MutationObserver((mutations) => {
each(mutations, (mutation) => {
this._handleMutation(mutation)
})
})
}
_handleMutation(mutation) {
if (isErudaEl(mutation.target)) return
const checkEl = (el) => {
const tagName = getLowerCaseTagName(el)
switch (tagName) {
case 'script':
this.refreshScript()
break
case 'img':
this.refreshImage()
break
case 'link':
this.refreshStylesheet()
break
}
}
if (mutation.type === 'attributes') {
checkEl(mutation.target)
} else if (mutation.type === 'childList') {
checkEl(mutation.target)
let nodes = toArr(mutation.addedNodes)
nodes = concat(nodes, toArr(mutation.removedNodes))
for (const node of nodes) {
checkEl(node)
}
}
}
_enableObserver() {
this._observer.observe(document.documentElement, {
attributes: true,
childList: true,
subtree: true,
})
}
_disableObserver() {
this._observer.disconnect()
}
}
function getLowerCaseTagName(el) {
if (!el.tagName) return ''
return el.tagName.toLowerCase()
}
const regImg = /\.(jpeg|jpg|gif|png)$/
const isImg = (url) => regImg.test(url)
|