2022-02-06 00:37:31 +08:00
|
|
|
|
|
2022-02-06 02:40:45 +08:00
|
|
|
|
var jcrop, selection
|
2022-02-06 00:37:31 +08:00
|
|
|
|
var relativePath = 'assets/html/screenshot'
|
|
|
|
|
|
|
|
|
|
var overlay = ((active) => (state) => {
|
|
|
|
|
active = typeof state === 'boolean' ? state : state === null ? active : !active
|
|
|
|
|
$('.jcrop-holder')[active ? 'show' : 'hide']()
|
2022-02-06 02:40:45 +08:00
|
|
|
|
chrome.runtime.sendMessage({ message: 'active', senderId: "screenshot", active })
|
2022-02-06 00:37:31 +08:00
|
|
|
|
})(false)
|
|
|
|
|
|
|
|
|
|
var image = (done) => {
|
|
|
|
|
var image = new Image()
|
|
|
|
|
image.id = 'fake-image'
|
|
|
|
|
image.src = chrome.runtime.getURL(relativePath + '/images/pixel.png')
|
|
|
|
|
image.onload = () => {
|
|
|
|
|
$('body').append(image)
|
|
|
|
|
done()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var init = (done) => {
|
|
|
|
|
$('#fake-image').Jcrop({
|
|
|
|
|
bgColor: 'none',
|
|
|
|
|
onSelect: (e) => {
|
|
|
|
|
selection = e
|
|
|
|
|
capture()
|
|
|
|
|
},
|
|
|
|
|
onChange: (e) => {
|
|
|
|
|
selection = e
|
|
|
|
|
},
|
|
|
|
|
onRelease: (e) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
selection = null
|
|
|
|
|
}, 100)
|
|
|
|
|
}
|
2022-02-06 02:40:45 +08:00
|
|
|
|
}, function ready() {
|
2022-02-06 00:37:31 +08:00
|
|
|
|
jcrop = this
|
|
|
|
|
|
|
|
|
|
$('.jcrop-hline, .jcrop-vline').css({
|
|
|
|
|
backgroundImage: `url(${chrome.runtime.getURL(relativePath + '/images/Jcrop.gif')})`
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (selection) {
|
|
|
|
|
jcrop.setSelect([
|
|
|
|
|
selection.x, selection.y,
|
|
|
|
|
selection.x2, selection.y2
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done && done()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var capture = (force) => {
|
|
|
|
|
chrome.storage.sync.get((config) => {
|
2022-02-06 02:40:45 +08:00
|
|
|
|
console.log(config)
|
2022-02-06 00:37:31 +08:00
|
|
|
|
if (selection && (config.method === 'crop' || (config.method === 'wait' && force))) {
|
|
|
|
|
jcrop.release()
|
|
|
|
|
setTimeout(() => {
|
2022-02-06 02:40:45 +08:00
|
|
|
|
console.log("准备capture")
|
2022-02-06 00:37:31 +08:00
|
|
|
|
chrome.runtime.sendMessage({
|
2022-02-06 02:40:45 +08:00
|
|
|
|
message: 'capture', senderId: "screenshot", area: selection, dpr: devicePixelRatio
|
2022-02-06 00:37:31 +08:00
|
|
|
|
}, (res) => {
|
2022-02-06 02:40:45 +08:00
|
|
|
|
console.log("capture回调结果:", res)
|
2022-02-06 02:42:55 +08:00
|
|
|
|
return // 变通
|
2022-02-06 00:37:31 +08:00
|
|
|
|
overlay(false)
|
|
|
|
|
selection = null
|
|
|
|
|
save(res.image, config.format, config.save)
|
|
|
|
|
})
|
|
|
|
|
}, 50)
|
|
|
|
|
}
|
|
|
|
|
else if (config.method === 'view') {
|
|
|
|
|
chrome.runtime.sendMessage({
|
2022-02-06 02:40:45 +08:00
|
|
|
|
message: 'capture', senderId: "screenshot",
|
|
|
|
|
area: { x: 0, y: 0, w: innerWidth, h: innerHeight }, dpr: devicePixelRatio
|
2022-02-06 00:37:31 +08:00
|
|
|
|
}, (res) => {
|
|
|
|
|
overlay(false)
|
|
|
|
|
save(res.image, config.format, config.save)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var filename = (format) => {
|
|
|
|
|
var pad = (n) => (n = n + '', n.length >= 2 ? n : `0${n}`)
|
|
|
|
|
var ext = (format) => format === 'jpeg' ? 'jpg' : format === 'png' ? 'png' : 'png'
|
|
|
|
|
var timestamp = (now) =>
|
|
|
|
|
[pad(now.getFullYear()), pad(now.getMonth() + 1), pad(now.getDate())].join('-')
|
|
|
|
|
+ ' - ' +
|
|
|
|
|
[pad(now.getHours()), pad(now.getMinutes()), pad(now.getSeconds())].join('-')
|
|
|
|
|
return `Screenshot Capture - ${timestamp(new Date())}.${ext(format)}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var save = (image, format, save) => {
|
2022-02-06 02:40:45 +08:00
|
|
|
|
var link = document.createElement('a')
|
|
|
|
|
link.download = filename(format)
|
|
|
|
|
link.href = image
|
|
|
|
|
link.click()
|
2022-02-06 00:37:31 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', ((timeout) => () => {
|
|
|
|
|
clearTimeout(timeout)
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
|
jcrop.destroy()
|
|
|
|
|
init(() => overlay(null))
|
|
|
|
|
}, 100)
|
|
|
|
|
})())
|
|
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener((req, sender, res) => {
|
2022-02-06 02:40:45 +08:00
|
|
|
|
console.log(`进入 assets\html\screenshot\js\content.js 中的onMessage Listener`)
|
|
|
|
|
if (req.senderId !== "screenshot") {
|
|
|
|
|
// 抛给下一个Listener
|
|
|
|
|
res();
|
|
|
|
|
}
|
2022-02-06 00:37:31 +08:00
|
|
|
|
if (req.message === 'init') {
|
|
|
|
|
res({}) // prevent re-injecting
|
|
|
|
|
|
|
|
|
|
if (!jcrop) {
|
|
|
|
|
image(() => init(() => {
|
|
|
|
|
overlay()
|
|
|
|
|
capture()
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
overlay()
|
|
|
|
|
capture(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-06 02:40:45 +08:00
|
|
|
|
console.log(`离开 assets\html\screenshot\js\content.js 中的onMessage Listener`)
|
2022-02-06 00:37:31 +08:00
|
|
|
|
return true
|
|
|
|
|
})
|