Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Edge support #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ build/chrome.zip: $(ADDON_DATA) addon/cr-background.html addon/cr-background.js
# Uncompressed due to prior difficulties with the webstore
cd build/chrome && zip -0 ../chrome.zip *

build/edge.zip: $(ADDON_DATA) addon/edge-background.html addon/edge-background.js addon/vendor/backgroundScriptsAPIBridge.js addon/vendor/contentScriptsAPIBridge.js
mkdir -p build/edge

sed "s/\/\*{{version}}\*\//$(VERSION)/" < addon/edge-manifest.json > build/edge/manifest.json

cp addon/edge-background.html build/edge/background.html
cp addon/edge-background.js build/edge/background.js

cp build/betterponymotes.js build/edge
cp build/bpm-resources.js build/edge
cp build/emote-classes.css build/edge
cp build/gif-animotes.css build/edge

cp addon/bootstrap.css build/edge
cp addon/bpmotes.css build/edge
cp addon/combiners-nsfw.css build/edge
cp addon/extracss-pure.css build/edge
cp addon/extracss-webkit.css build/edge
cp addon/options.css build/edge
cp addon/options.html build/edge
cp addon/options.js build/edge
cp addon/pref-setup.js build/edge
cp addon/vendor/backgroundScriptsAPIBridge.js build/edge
cp addon/vendor/contentScriptsAPIBridge.js build/edge

cp betterponymotes.pem build/edge/key.pem
# Uncompressed due to prior difficulties with the webstore
cd build/edge && zip -0 ../edge.zip *

build/BPM.safariextension: $(ADDON_DATA) addon/sf-Settings.plist addon/sf-background.html addon/sf-background.js
mkdir -p build/BPM.safariextension

Expand Down
23 changes: 18 additions & 5 deletions addon/bpm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ var _raw_log;

var CONTAINER_PADDING = 10;

if(_console && _console.log) {
// We define this up here becuase we have some restrictions on how/when
// we set up our logger, which happens _before_ we define our browser
function isEdge() {
return !!window.StyleMedia;
}

/* Edge doesn't allow you to bind _.console.log */
if(isEdge()) {
_raw_log = _console.log;
} else if(_console && _console.log) {
_raw_log = _console.log.bind(_console);
} else if(_gm_log) {
_raw_log = function() {
Expand All @@ -46,7 +55,7 @@ if(_console && _console.log) {

function _wrap_logger(cname, prefix, level) {
var cfunc;
if(_console && _console[cname]) {
if(_console && _console[cname] && !isEdge()) {
cfunc = _console[cname].bind(_console);
} else {
cfunc = _raw_log;
Expand All @@ -70,7 +79,7 @@ var log_info = _wrap_logger("log", "INFO:", _LOG_INFO);
var log_warning = _wrap_logger("warn", "WARNING:", _LOG_WARNING);
var log_error = _wrap_logger("error", "ERROR:", _LOG_ERROR);
var log_trace = function() {};
if(_console && _console.trace) {
if(_console && _console.trace && !isEdge()) {
log_trace = _console.trace.bind(_console);
}

Expand All @@ -89,6 +98,9 @@ if(self.on) {
platform = "chrome-ext";
} else if(find_global("safari") && window.name === "") {
platform = "safari-ext";
// Edge works via an MS-provided compatibility layer that emulates Chrome's API.
} else if(isEdge()) {
platform = "chrome-ext";
} else {
log_error("Unknown platform! Your installation is badly broken.");
platform = "unknown";
Expand All @@ -111,9 +123,10 @@ function catch_errors(f) {
return f.apply(this, arguments);
} catch(e) {
log_error("Exception on line " + e.lineNumber + ": ", e.name + ": " + e.message);
if(e.trace) {
if(e.trace || e.stack) {
log_error("Stack trace:");
log_error(e.trace);
// This is apparently `e.stack` in Edge
log_error(e.trace || e.stack);
} else {
log_error("Current stack:");
// This probably isn't very useful...
Expand Down
13 changes: 13 additions & 0 deletions addon/edge-background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>BetterPonymotes Edge Background Page</title>
<script src="bpm-resources.js" type="text/javascript"></script>
<script src="pref-setup.js" type="text/javascript"></script>
<script src="background.js" type="text/javascript"></script>
</head>
<body>
BetterPonymotes
</body>
</html>
95 changes: 95 additions & 0 deletions addon/edge-background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*******************************************************************************
**
** This file is part of BetterPonymotes.
** Copyright (c) 2012-2015 Typhos.
**
** This program is free software: you can redistribute it and/or modify it
** under the terms of the GNU Affero General Public License as published by
** the Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This program is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
** for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

"use strict";

if(localStorage.prefs === undefined) {
localStorage.prefs = "{}";
}

var pref_manager = manage_prefs(sr_name2id, {
read_value: function(key) { return localStorage[key]; },
write_value: function(key, data) { localStorage[key] = data; },
read_json: function(key) { return localStorage[key] === undefined ? undefined : JSON.parse(localStorage[key]); },
write_json: function(key, data) { localStorage[key] = JSON.stringify(data); },

download_file: function(done, url, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState === 4) {
done();
var type = request.getResponseHeader("Content-Type");
if(request.status === 200 && type == "text/css") {
callback(request.responseText);
} else {
console.log("BPM: ERROR: Reddit returned HTTP status " + request.status + " for " + url + " (type: " + type + ")");
}
}
};
request.open("GET", url, true);
// Not permitted because Chrome sucks
//request.setRequestHeader("User-Agent", "BetterPonymotes Client CSS Updater (/u/Typhos)");
request.send();
},

// Chrome's setTimeout() does not appreciate a this parameter
set_timeout: setTimeout.bind(undefined)
});

// Content script requests
browser.runtime.onMessage.addListener(function(message, sender, sendResponse) {
switch(message.method) {
case "get_initdata":
var reply = {"method": "initdata"};
if(message.want["prefs"]) {
reply.prefs = pref_manager.get();
}
if(message.want["customcss"]) {
reply.emotes = pref_manager.cm.emote_cache;
reply.css = pref_manager.cm.css_cache;
}
sendResponse(reply);
break;

case "get_prefs":
sendResponse({"method": "prefs", "prefs": pref_manager.get()});
break;

case "get_custom_css":
sendResponse({
"method": "custom_css",
"css": pref_manager.cm.css_cache,
"emotes": pref_manager.cm.emote_cache
});
break;

case "set_pref":
pref_manager.set_pref(message.pref, message.value);
break;

case "force_update":
pref_manager.cm.force_update(message.subreddit);
break;

default:
console.log("BPM: ERROR: Unknown request from content script: '" + message.request + "'");
break;
}
});
49 changes: 49 additions & 0 deletions addon/edge-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"author": "Typhos",
"background": {
"page": "background.html",
"persistent": false
},
"content_scripts": [
{
"all_frames": true,
"js": [
"bpm-resources.js",
"betterponymotes.js"
],
"matches": [
"*://*/*"
],
"run_at": "document_end"
}
],
"description": "View Reddit ponymotes across the site",
"homepage_url": "https://ponymotes.net/bpm/",
"manifest_version": 2,
"minimum_edge_version": "33.14281.1000.0",
"minimum_chrome_version": "20",
"-ms-preload": {
"backgroundScript": "backgroundScriptsAPIBridge.js",
"contentScript": "contentScriptsAPIBridge.js"
},
"name": "BetterPonymotes",
"options_page": "options.html",
"permissions": [
"http://*.reddit.com/",
"https://*.reddit.com/",
"http://*.redditstatic.com/",
"https://*.redditstatic.com/",
"http://*.redditmedia.com/",
"https://*.redditmedia.com/"
],
"version": "/*{{version}}*/",
"web_accessible_resources": [
"bpmotes.css",
"emote-classes.css",
"combiners-nsfw.css",
"extracss-pure.css",
"extracss-webkit.css",
"gif-animotes.css",
"options.html"
]
}
Loading