mirror of
https://gitee.com/bitdance-team/chrome-extension
synced 2025-10-08 08:45:14 +08:00
截图功能基本ok,代码可能还有小修改
This commit is contained in:
121
packages/shell-chrome/js/content.js
Normal file
121
packages/shell-chrome/js/content.js
Normal file
@@ -0,0 +1,121 @@
|
||||
|
||||
var jcrop,selection
|
||||
|
||||
var overlay = ((active) => (state) => {
|
||||
active = typeof state === 'boolean' ? state : state === null ? active : !active
|
||||
$('.jcrop-holder')[active ? 'show' : 'hide']()
|
||||
chrome.runtime.sendMessage({message: 'active', active})
|
||||
})(false)
|
||||
|
||||
var image = (done) => {
|
||||
var image = new Image()
|
||||
image.id = 'fake-image'
|
||||
image.src = chrome.runtime.getURL('/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)
|
||||
}
|
||||
}, function ready () {
|
||||
jcrop = this
|
||||
|
||||
$('.jcrop-hline, .jcrop-vline').css({
|
||||
backgroundImage: `url(${chrome.runtime.getURL('/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) => {
|
||||
if (selection && (config.method === 'crop' || (config.method === 'wait' && force))) {
|
||||
jcrop.release()
|
||||
setTimeout(() => {
|
||||
chrome.runtime.sendMessage({
|
||||
message: 'capture', area: selection, dpr: devicePixelRatio
|
||||
}, (res) => {
|
||||
overlay(false)
|
||||
selection = null
|
||||
save(res.image, config.format, config.save)
|
||||
})
|
||||
}, 50)
|
||||
}
|
||||
else if (config.method === 'view') {
|
||||
chrome.runtime.sendMessage({
|
||||
message: 'capture',
|
||||
area: {x: 0, y: 0, w: innerWidth, h: innerHeight}, dpr: devicePixelRatio
|
||||
}, (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) => {
|
||||
var link = document.createElement('a')
|
||||
link.download = filename(format)
|
||||
link.href = image
|
||||
link.click()
|
||||
|
||||
}
|
||||
|
||||
window.addEventListener('resize', ((timeout) => () => {
|
||||
clearTimeout(timeout)
|
||||
timeout = setTimeout(() => {
|
||||
jcrop.destroy()
|
||||
init(() => overlay(null))
|
||||
}, 100)
|
||||
})())
|
||||
|
||||
chrome.runtime.onMessage.addListener((req, sender, res) => {
|
||||
if (req.message === 'init') {
|
||||
res({}) // prevent re-injecting
|
||||
|
||||
if (!jcrop) {
|
||||
image(() => init(() => {
|
||||
overlay()
|
||||
capture()
|
||||
}))
|
||||
}
|
||||
else {
|
||||
overlay()
|
||||
capture(true)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
7
packages/shell-chrome/js/jquery.Jcrop.min.js
vendored
Normal file
7
packages/shell-chrome/js/jquery.Jcrop.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
4
packages/shell-chrome/js/jquery.min.js
vendored
Normal file
4
packages/shell-chrome/js/jquery.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user