forked from MrXujiang/fetchImg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
102 lines (93 loc) · 2.95 KB
/
content_script.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
$('body').append("<div class='crawl-btn'>提取</div>")
$('.crawl-btn').click(function(){
var imgArr = []
$('img').each(function(i) {
var src = $(this).attr('src');
var realSrc = /^(http|https)/.test(src) ? src : location.protocol+ '//' + location.host + src;
imgArr.push(realSrc)
})
var imgBox = $("<div class='img-box'></div>");
imgArr.forEach(item => {
var imgWrap = $("<div class='img-wrap'></div>");
var img = $("<img src='" + item + "' />");
imgWrap.append(img);
imgBox.append(imgWrap);
})
console.log(imgArr)
chrome.storage.sync.set({'imgArr': imgArr}, function() {
// Notify that we saved.
console.log('保存成功');
// chrome.pageAction.setBadgeText({text: '新增'});
// chrome.pageAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
});
Modal.show({
title: '提取结果',
content: imgBox
})
})
// 弹窗
~function Modal() {
var modal;
if(this instanceof Modal) {
this.init = function(opt) {
modal = $("<div class='modal'></div>");
var title = $("<div class='modal-title'>" + opt.title + "</div>");
var close_btn = $("<span class='modal-close-btn'>X</span>");
var content = $("<div class='modal-content'></div>");
var mask = $("<div class='modal-mask'></div>");
close_btn.click(function(){
modal.hide()
})
title.append(close_btn);
content.append(title);
content.append(opt.content);
modal.append(content);
modal.append(mask);
$('body').append(modal);
}
this.show = function(opt) {
if(modal) {
modal.show();
}else {
var options = {
title: opt.title || '标题',
content: opt.content || ''
}
this.init(options)
modal.show();
}
}
this.hide = function() {
modal.hide();
}
}else {
window.Modal = new Modal()
}
}()
// var imgURL = chrome.runtime.getURL("/images/icon48.png");
// $('img')[0].src = imgURL;
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if (message == "activeBtn"){
if(!$('.crawl-btn')) {
$('body').append("<div class='crawl-btn'>提取</div>")
}else {
$('.crawl-btn').css("background-color","orange");
setTimeout(() => {
$('.crawl-btn').css("background-color","#06c");
}, 3000);
}
sendResponse({farewell: "激活成功"});
}
});
// chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
// console.log(response, document.body);
// // document.body.style.backgroundColor="orange"
// });
// 在页面上插入代码
// 在配置文件中需设置"persistent": true, 因为事件页面不支持webrequest
console.log(11111, chrome.extension.getURL('ajax_interceptor.js'))
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', chrome.extension.getURL('ajax_interceptor.js'));
$('body').append(script);