summaryrefslogtreecommitdiff
path: root/scraper/content-script/background.js
diff options
context:
space:
mode:
Diffstat (limited to 'scraper/content-script/background.js')
-rw-r--r--scraper/content-script/background.js135
1 files changed, 135 insertions, 0 deletions
diff --git a/scraper/content-script/background.js b/scraper/content-script/background.js
new file mode 100644
index 00000000..78244b12
--- /dev/null
+++ b/scraper/content-script/background.js
@@ -0,0 +1,135 @@
+// this script talks to the button, tells whether it is off or on,
+// and communicates this state to the open tabs
+
+var on = true;
+var TYPES = { Status: 0, Connected: 1, Disconnected: 2, Image: 3, Text: 4 }
+
+chrome.browserAction.onClicked.addListener(function(tab) {
+ on = ! on
+ if (on) {
+ localStorage.setItem("megapixels", "on")
+ chrome.browserAction.setIcon({path: 'alone-on.png'})
+ chrome.browserAction.setTitle({title: "Stop Mego"});
+ chrome.tabs.getSelected(null, function(tab) {
+ chrome.tabs.sendMessage(tab.id, {method:"start"}, function(response){})
+ })
+ }
+ else {
+ localStorage.setItem("megapixels", "off")
+ chrome.browserAction.setIcon({path: 'alone-off.png'})
+ chrome.browserAction.setTitle({title: "Start Mego"})
+ chrome.tabs.getSelected(null, function(tab) {
+ chrome.tabs.sendMessage(tab.id, {method:"stop"}, function(response){})
+ })
+ }
+});
+
+// websocket boilerplate
+// var ws, ws_timeout, ws_delay = 500, opt = {}
+// function reset () {
+// close()
+// clearTimeout(ws_timeout)
+// ws_delay = Math.min(ws_delay*2, 60000)
+// ws_timeout = setTimeout(open, ws_delay)
+// }
+// function open () {
+// close()
+// ws = new WebSocket('ws://' + opt.server + ':' + opt.port)
+// ws.onopen = function (event) {
+// ws_delay = 500
+// send({ type: TYPES.Connected, data: { client: "browser" } })
+// console.log('websocket open')
+// }
+// ws.onerror = function () {
+// reset()
+// }
+// ws.onclose = function () {
+// reset()
+// }
+// }
+// function close () {
+// try {
+// ws.close()
+// ws = null
+// } catch (e) {}
+// }
+// function send (msg, force) {
+// msg.name = opt.name
+// if ( (on || force) && ws ) ws.send(JSON.stringify(msg))
+// }
+
+// // load options and connect websocket
+// chrome.storage.sync.get({
+// server: 'localhost',
+// port: 3000,
+// name: 'whomever',
+// }, function(items) {
+// console.log(items)
+// "server port name".split(" ").forEach(function(id){
+// opt[id] = items[id]
+// })
+// open()
+// });
+
+// chrome.storage.onChanged.addListener(function(changes, ns) {
+// for (key in changes) {
+// var change = changes[key]
+// opt[key] = change.newValue
+// }
+// open()
+// });
+
+// // listen for image urls
+// chrome.webRequest.onResponseStarted.addListener(function(details) {
+// send({ type: TYPES.Image, data: details.url })
+// }, {
+// urls: ["<all_urls>"],
+// types: ["image"]
+// }, ["responseHeaders"]);
+
+function push(s){
+ console.log(s)
+ let d = localStorage.getItem('site')
+ if (s === 'done') {
+ download('[' + localStorage.getItem('site') + ']')
+ return
+ }
+ if (d) s = d + ',' + s
+ localStorage.setItem('site', s)
+}
+function reset(){
+ localStorage.setItem('site', '')
+}
+function download(content, fileName, contentType) {
+ var a = document.createElement("a");
+ var file = new Blob([JSON.stringify(content)], {type: contentType});
+ a.href = URL.createObjectURL(file);
+ a.download = fileName;
+ a.click();
+}
+
+// new pages will check if we're listening
+chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
+ console.log(request)
+ switch (request.type) {
+ case TYPES.Status:
+ sendResponse({ status: localStorage['megapixels'] })
+ break
+ case TYPES.Text:
+ push(request.data)
+ break
+ }
+});
+
+// injects check.js into tabs if not already loaded
+chrome.tabs.query({}, function(tabs) {
+ for (var i = 0; i < tabs.length; i++) {
+ var tabid = tabs[i].id;
+ if (tabs[i].url.indexOf("chrome") === -1) {
+ chrome.tabs.executeScript(tabid, {
+ file: "check.js"
+ })
+ }
+ }
+})
+