diff options
Diffstat (limited to 'content-script')
| -rw-r--r-- | content-script/.gitignore | 2 | ||||
| -rw-r--r-- | content-script/alone-off.png | bin | 0 -> 1259 bytes | |||
| -rw-r--r-- | content-script/alone-on.png | bin | 0 -> 1271 bytes | |||
| -rw-r--r-- | content-script/background.js | 135 | ||||
| -rw-r--r-- | content-script/check.js | 102 | ||||
| -rw-r--r-- | content-script/icon-128.png | bin | 0 -> 15156 bytes | |||
| -rw-r--r-- | content-script/icon-16.png | bin | 0 -> 1490 bytes | |||
| -rw-r--r-- | content-script/icon-48.png | bin | 0 -> 3973 bytes | |||
| -rw-r--r-- | content-script/index.html | 8 | ||||
| -rw-r--r-- | content-script/manifest.json | 29 | ||||
| -rw-r--r-- | content-script/options.html | 16 | ||||
| -rw-r--r-- | content-script/options.js | 34 |
12 files changed, 326 insertions, 0 deletions
diff --git a/content-script/.gitignore b/content-script/.gitignore new file mode 100644 index 00000000..5ca0973f --- /dev/null +++ b/content-script/.gitignore @@ -0,0 +1,2 @@ +.DS_Store + diff --git a/content-script/alone-off.png b/content-script/alone-off.png Binary files differnew file mode 100644 index 00000000..677b2273 --- /dev/null +++ b/content-script/alone-off.png diff --git a/content-script/alone-on.png b/content-script/alone-on.png Binary files differnew file mode 100644 index 00000000..7c0f441e --- /dev/null +++ b/content-script/alone-on.png diff --git a/content-script/background.js b/content-script/background.js new file mode 100644 index 00000000..78244b12 --- /dev/null +++ b/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" + }) + } + } +}) + diff --git a/content-script/check.js b/content-script/check.js new file mode 100644 index 00000000..0644084d --- /dev/null +++ b/content-script/check.js @@ -0,0 +1,102 @@ +(function(){ + + var listening = false, loaded = false + var TYPES = { Status: 0, Connected: 1, Disconnected: 2, Image: 3, Text: 4 } + var PUNCTUATION_REGEX = /[\[\]\{\}]/g // i.e. resembles code + var NULL_ARRAY = [] + var seen = {} + const toArray = (a) => Array.prototype.slice.call(a) + const $ = (s) => document.querySelector(s) + const $$ = (s) => document.querySelectorAll(s) + + function init () { + if (window.location.href.indexOf("lvh.me") !== -1 || window.location.href.indexOf("localhost") !== -1) return + bind() + } + function bind () { + chrome.extension.onMessage.addListener(onMessage) + chrome.extension.sendMessage({ type: TYPES.Status }, gotStatus) + } + function gotStatus (response) { + console.log('got status', response) + if (response && response.status === "on") { + console.log('its on', loaded) + if (! loaded) { + console.log('started') + setTimeout(() => { start() }, 5000) + // send(document.body.innerText) + // setInterval(function(){ + // send(document.body.innerText) + // }, 10000) + } + loaded = true + } + } + function onMessage (request, sender, sendResponse) { + switch (request.method) { + case 'start': + start() + break + case 'stop': + listening = false + break + } + } + function start(){ + console.log(window.location.href, listening) + if (window.location.href.indexOf('schol' + 'ar' + '.go' + 'og' + 'le') === -1) return + if (listening) return + listening = true + energize() + } + + function energize(){ + console.log('energize') + const records = toArray($$(".gs_r")).map((el, i) => { + let data = {} + let link = el.querySelector("h3 a") + if (link) { + data.link = link.href + } + let pdfLink = el.querySelector(".gs_or_ggsm a") + if (pdfLink) { + data.pdfLink = pdfLink.href + } + let attribution = el.querySelector('.gs_a') + if (attribution) { + data.attribution = attribution.innerText + data.attributionLinks = toArray(attribution.querySelectorAll('a')).map(a => ({ + href: a.href, + name: a.innerText, + })) + } + let snippet = el.querySelector('.gs_a') + if (snippet) { + data.snippet = snippet.innerText + } + let citationLink = el.querySelector('.gs_fl a:nth-of-type(3)') + if (citationLink && citationLink.innerText.match('Cited by')) { + data.citationLink = citationLink.href + data.citationCount = parseInt(citationLink.innerText.replace(/^\s*Cited by /, ''), 10) || -1 + } + return data + }) + let record = { + title: document.querySelector('title').innerText, + url: window.location.href, + records: records, + } + send(JSON.stringify(record)) + let nextLink = $("#gs_n td:last-child a") + if (nextLink) { + setTimeout(() => { nextLink.click() }, 19000 + (Math.random() * 21000)) + } + send("done") + } + function send (text) { + chrome.extension.sendMessage({ type: TYPES.Text, data: text }, function(){}) + } + + init() + +})() diff --git a/content-script/icon-128.png b/content-script/icon-128.png Binary files differnew file mode 100644 index 00000000..75ccfc80 --- /dev/null +++ b/content-script/icon-128.png diff --git a/content-script/icon-16.png b/content-script/icon-16.png Binary files differnew file mode 100644 index 00000000..b2bfd098 --- /dev/null +++ b/content-script/icon-16.png diff --git a/content-script/icon-48.png b/content-script/icon-48.png Binary files differnew file mode 100644 index 00000000..b4b752a7 --- /dev/null +++ b/content-script/icon-48.png diff --git a/content-script/index.html b/content-script/index.html new file mode 100644 index 00000000..577ab5b0 --- /dev/null +++ b/content-script/index.html @@ -0,0 +1,8 @@ +<!doctype html> +<html> +<head> +<script type="text/javascript" src="background.js"></script> +</head> +<body> +</body> +</html> diff --git a/content-script/manifest.json b/content-script/manifest.json new file mode 100644 index 00000000..dc7e9c9b --- /dev/null +++ b/content-script/manifest.json @@ -0,0 +1,29 @@ +{ + "name": "Mego", + "version": "1.0.0", + "manifest_version": 2, + "description": "Mego content extension", + "homepage_url": "https://asdf.us/", + "icons": { "16": "icon-16.png", + "48": "icon-48.png", + "128": "icon-128.png" }, + "permissions": ["storage", "tabs", "webRequest", "*://*/*"], + "background": { + "page": "index.html" + }, + "content_scripts": [ + { + "matches": ["*://*/*"], + "js": ["check.js"] + } + ], + "browser_action": { + "name": "Start Mego", + "default_icon": "alone-on.png", + "icons": ["alone-on.png", "alone-off.png"] + }, + "options_ui": { + "page": "options.html", + "chrome_style": true + } +} diff --git a/content-script/options.html b/content-script/options.html new file mode 100644 index 00000000..8b5ba90c --- /dev/null +++ b/content-script/options.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html> +<head> + <title>Alone Together Plugin Options</title> + <style> + body { padding: 10px; } + label { min-width: 90px; display: inline-block; } + #port { width: 50px; } + div { margin-bottom: 10px; } + </style> +</head> + +<body> +</body> + +</html>
\ No newline at end of file diff --git a/content-script/options.js b/content-script/options.js new file mode 100644 index 00000000..ef9396fa --- /dev/null +++ b/content-script/options.js @@ -0,0 +1,34 @@ +// Saves options to chrome.storage.sync. +function $(n) { return document.querySelector(n) } +function save_options() { + chrome.storage.sync.set({ + server: $("#server").value, + port: parseInt( $("#port").value, 10 ), + name: $("#name").value, + }, function() { + // Update status to let user know options were saved. + var status = document.getElementById('status'); + status.textContent = 'Options saved.'; + setTimeout(function() { + status.textContent = ''; + }, 750); + }); +} + +// Restores select box and checkbox state using the preferences +// stored in chrome.storage. +function restore_options() { + chrome.storage.sync.get({ + server: 'localhost', + port: 3000, + name: 'whomever', + }, function(items) { + console.log(items) + "server port name".split(" ").forEach(function(id){ + $("#"+id).value = items[id] + }) + }); +} +document.addEventListener('DOMContentLoaded', restore_options); +document.getElementById('save').addEventListener('click', save_options); +console.log("WHAT") |
