File size: 5,171 Bytes
f5baa4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    "id": "copyCookie",
    "title": "复制cookie",
    "contexts": ["page"]
  });
  chrome.contextMenus.create({
    "id": "pushCookie",
    "title": "推送cookie",
    "contexts": ["page"]
  });
  chrome.contextMenus.create({
    "id": "clearCookie",
    "title": "清除cookie",
    "contexts": ["page"]
  });
});


let getCurrentTab = async () => {
  let queryOptions = {
    active: true,
    currentWindow: true
  };
  let [tab] = await chrome.tabs.query(queryOptions);
  return tab;
}

let spliceCookies = (cookies) => {
  return cookies.map(c => c.name + '=' + c.value).join('; ')
}

let copyCookies = (tag, cookies) => {
  const cookieStr = spliceCookies(cookies)
  chrome.scripting.executeScript({
    target: {
      tabId: tag.id
    },
    func: (val) => navigator.clipboard.writeText(val),
    args: [cookieStr]
  }, () => {
    console.log('cookie复制成功', cookieStr)
    chrome.notifications.create({
      type: "basic",
      title: "cookie复制成功",
      message: "cookie已成功复制到剪切板",
      iconUrl: "/images/icon-128.png"
    })
  });
}

let pushCookies = (cookies) => {
  chrome.storage.sync.get(['url', 'method', 'fieldName', 'fieldLocation'], config => {
    if (!config.url) {
      chrome.notifications.create({
        type: "basic",
        title: "cookie推送失败",
        message: "未配置服务器信息",
        iconUrl: "/images/icon-128.png"
      })
      return
    }
    let cookieStr = spliceCookies(cookies)
    console.log("推送配置", config)
    console.log("推送cookie", cookieStr)
    let request;
    switch (config.fieldLocation) {
      case 'header':
        let headers = {}
        headers[config.fieldName] = cookieStr
        request = fetch(config.url, {
          method: config.method,
          headers
        })
        break;
      case 'url':
        request = fetch(`${config.url}?${config.fieldName}=${cookieStr}`, {
          method: config.method
        })
        break;
      case 'body':
        request = fetch(config.url, {
          method: config.method,
          headers: {
            "Content-type": "application/json;charset=UTF-8",
          },
          body: `{"${config.fieldName}":"${cookieStr}"}`,
        })
        break;
    }
    request.then(r => r.text()).then(text => {
      console.log("返回结果", text)
      chrome.notifications.create({
        type: "basic",
        title: "cookie推送结果",
        message: text,
        iconUrl: "/images/icon-128.png"
      })
    }, err => {
      console.error("请求失败", err);
      chrome.notifications.create({
        type: "basic",
        title: "cookie推送出错",
        message: err.message,
        iconUrl: "/images/icon-128.png"
      })
    });
  })
}

let removeCookies = (cookies, url) => {
  for (const cookie of cookies) {
    chrome.cookies.remove({
      name: cookie.name,
      url
    })
  }
  chrome.notifications.create({
    type: "basic",
    title: "cookie清除成功",
    message: "cookie已全部清空",
    iconUrl: "/images/icon-128.png"
  })
}

chrome.contextMenus.onClicked.addListener(async (itemData) => {
  let tag = await getCurrentTab();
  let cookies = await chrome.cookies.getAll({
    url: tag.url
  })
  switch (itemData.menuItemId) {
    case 'copyCookie':
      copyCookies(tag, cookies)
      break;
    case 'pushCookie':
      pushCookies(cookies)
      break;
    case 'clearCookie':
      removeCookies(cookies, tag.url)
      break;
  }
});

var lastAutoPushTime

let cookieListener = (changeInfo) => {
  if (changeInfo.removed) {
    //过滤删除cookie的操作
    return
  }
  chrome.storage.sync.get(['listenerUrl', 'minPushInterval', 'cookieNames'], async (config) => {
    if (!config.cookieNames.includes(changeInfo.cookie.name)) {
      //不在监控的cookie名称之中,不推送
      return
    }
    const topDomain = changeInfo.cookie.domain.split('.').slice(-2).join('.');
    if (!config.listenerUrl.includes(topDomain)) {
      //不在同一域名下,不推送
      return
    }
    const timeInMs = Date.now()
    if (lastAutoPushTime && (timeInMs - lastAutoPushTime) / 1000 < config.minPushInterval) {
      //还没到最短间隔时长,不推送
      return
    }
    lastAutoPushTime = timeInMs
    setTimeout(function () {
      //延迟3秒再推送(网站可能一次性设置了很多cookie,立即获取得到的cookie可能不完整)
      chrome.cookies.getAll({
        url: config.listenerUrl
      },cookies=>pushCookies(cookies))
  }, 3000);
  });
}

chrome.storage.sync.get(['isAutoPush'], (result) => {
  if (result.isAutoPush) {
    console.log('开始监听cookie')
    chrome.cookies.onChanged.addListener(cookieListener)
  }
})

chrome.storage.onChanged.addListener((changes) => {
  if (changes.isAutoPush) {
    let newVal = changes.isAutoPush.newValue
    if (newVal) {
      console.log('开始监听cookie')
      chrome.cookies.onChanged.addListener(cookieListener)
    } else {
      console.log('取消监听cookie')
      chrome.cookies.onChanged.removeListener(cookieListener)
    }
  }
})