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

Port to safari #62

Open
wants to merge 3 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
Binary file added Icon-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Author</key>
<string>Bountysource</string>
<key>Builder Version</key>
<string>11601.7.7</string>
<key>CFBundleDisplayName</key>
<string>Bountysource</string>
<key>CFBundleIdentifier</key>
<string>com.bountysource.safari-extension</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleShortVersionString</key>
<string>0.0.14</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
<integer>1048576</integer>
<key>Global Page</key>
<string>global.html</string>
<key>Toolbar Items</key>
<array>
<dict>
<key>Command</key>
<string>show_popover</string>
<key>Identifier</key>
<string>bountysource-button</string>
<key>Image</key>
<string>data/images/thumbsup-16.png</string>
<key>Include By Default</key>
<true/>
<key>Label</key>
<string>👍</string>
<key>Palette Label</key>
<string>Bountysource exstension</string>
</dict>
</array>
</dict>
<key>Content</key>
<dict>
<key>Scripts</key>
<dict>
<key>End</key>
<array>
<string>data/get_safari_cookie.js</string>
<string>data/thumbs.js</string>
</array>
<key>Start</key>
<array>
<string>data/bountysource_client.js</string>
</array>
</dict>
<key>Stylesheets</key>
<array>
<string>data/stylesheets/thumbs.css</string>
</array>
</dict>
<key>Description</key>
<string>Bountysource Browser Extension</string>
<key>DeveloperIdentifier</key>
<string>0000000000</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
<dict>
<key>Website Access</key>
<dict>
<key>Include Secure Pages</key>
<true/>
<key>Level</key>
<string>All</string>
</dict>
</dict>
<key>Website</key>
<string>https://www.bountysource.com</string>
</dict>
</plist>
28 changes: 27 additions & 1 deletion data/bountysource_client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Cross-browser helpers to be used in popups and content-scripts
var BountysourceClient = {
browser: typeof(chrome)!=='undefined' ? 'chrome' : 'firefox',
browser: typeof(chrome)!=='undefined' ? 'chrome' : typeof(safari)!=='undefined' ? 'safari' : 'firefox',

// Computes path to extension hosted image
imagePath: function(image) {
if (window.chrome) {
return chrome.extension.getURL('data/images/' + image);
} else if (self && self.options && self.options.image_base) {
return self.options.image_base + image;
} else if (window.safari) {
return safari.extension.baseURI + 'data/images/' + image;
}
},

Expand All @@ -26,6 +28,30 @@ var BountysourceClient = {
options.callback_str = "callback_" + (new Date()).getTime();
self.port.once(options.callback_str, callback);
self.port.emit("message", options);
} else if (BountysourceClient.browser === 'safari') {
// Check if is sending from popover or normal page
if (safari.self.tab) {
// Is normal page
// Equivalent code of self.port.once
options.callback_str = "callback_" + (new Date()).getTime();
var handler = function(event) {
if (event.name === options.callback_str) {
callback(event.message);
safari.self.removeEventListener('message', handler);
}
}
safari.self.addEventListener('message', handler, false);
safari.self.tab.dispatchMessage('message', options);
} else {
// Is popover
var BountysourceServer = safari.extension.globalPage.contentWindow.BountysourceServer;
BountysourceServer.get_access_token(function(access_token) {
options.access_token = access_token;
options.callback = callback;

BountysourceServer[options.action](options);
});
}
}
},

Expand Down
3 changes: 3 additions & 0 deletions data/get_safari_cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (document.location.host === 'www.bountysource.com') {
safari.self.tab.dispatchMessage('cookie', document.cookie);
}
21 changes: 21 additions & 0 deletions data/popup/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var BountysourcePopup = {
BountysourcePopup.setupTabHandlers();
BountysourcePopup.setupFeedbackForm();
BountysourcePopup.loadThumbs();
BountysourcePopup.setupLinks();
BountysourcePopup.setupForms();
},

loadCurrentPerson: function() {
Expand Down Expand Up @@ -122,6 +124,7 @@ var BountysourcePopup = {
document.querySelector('#thumbs-message-none').classList.remove('hide');
}

BountysourcePopup.setupLinks();
BountysourcePopup.resizePopup();
});
},
Expand Down Expand Up @@ -183,6 +186,24 @@ var BountysourcePopup = {
document.querySelector('#feedback-form').classList.add('hide');
BountysourcePopup.resizePopup();
});
},

setupLinks: function() {
if ((typeof(safari) !== 'undefined')) {
var links = document.querySelectorAll('a[target="_blank"]');
for (var i = 0; i < links.length; i++) {
links[i].removeAttribute('target');
}
}
},

setupForms: function() {
if ((typeof(safari) !== 'undefined')) {
var links = document.querySelectorAll('form[target="_blank"]');
for (var i = 0; i < links.length; i++) {
links[i].removeAttribute('target');
}
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion data/popup/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(function() {
var scripts = ['../bountysource_client.js', 'application.js'];

if (typeof(chrome) !== 'undefined') {
if (typeof(chrome) !== 'undefined' || typeof(safari) !== 'undefined') {
for (var i=0; i < scripts.length; i++) {
var script_tag = document.createElement('script');
script_tag.src = scripts[i];
Expand Down
9 changes: 9 additions & 0 deletions global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE HTML>
<html>
<head>
<title>global HTML page</title>
<script type="text/javascript" src="./lib/bountysource_server.js"></script>
</head>
<body>
</body>
</html>
63 changes: 53 additions & 10 deletions lib/bountysource_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (typeof(localStorage) === 'undefined') {

// global functions
var BountysourceServer = {
browser: typeof(chrome)!=='undefined' ? 'chrome' : 'firefox',
browser: typeof(chrome)!=='undefined' ? 'chrome' : typeof(safari)!=='undefined' ? 'safari' : 'firefox',

www_base: 'https://www.bountysource.com/',
api_base: 'https://api.bountysource.com/',
Expand Down Expand Up @@ -64,6 +64,24 @@ var BountysourceServer = {
});
}
});
} else if (BountysourceServer.browser === 'safari') {
safari.application.addEventListener('message', function(event) {
if (event.name === 'message') {
BountysourceServer.get_access_token(function(access_token) {
var options = event.message;
options.access_token = access_token;

// turn callback_str into function
options.callback = function(response) {
event.target.page.dispatchMessage(options.callback_str, response);
};

BountysourceServer[options.action](options);
});
} else if (event.name === 'cookie') {
localStorage['safari_cookie'] = event.message;
}
}, false);
}
},

Expand Down Expand Up @@ -123,6 +141,26 @@ var BountysourceServer = {
}
}
});
} else if (BountysourceServer.browser === 'safari') {
safari.application.addEventListener('command', function(event) {
if (event.command === 'show_popover') {
var button;
for (var i = 0; i < safari.extension.toolbarItems.length; i++) {
if (safari.extension.toolbarItems[i].identifier === 'bountysource-button') {
button = safari.extension.toolbarItems[i];
}
}
if (button.popover) {
button.popover.hide();
button.popover = null;
safari.extension.removePopover('bountysource-popover');
}

popover = safari.extension.createPopover('bountysource-popover', safari.extension.baseURI + 'data/popup/application.html', 420);
button.popover = popover;
button.showPopover();
}
}, false);
}
},

Expand Down Expand Up @@ -195,15 +233,9 @@ var BountysourceServer = {
var cookieUri = ios.newURI(BountysourceServer.www_base, null, null);
var cookieSvc = ff_chrome.components.classes["@mozilla.org/cookieService;1"].getService(ff_chrome.components.interfaces.nsICookieService);
var cookie = cookieSvc.getCookieString(cookieUri, null);
var cookies = (cookie || '').split('; ');
for (var i=0; i < cookies.length; i++) {
var regex_match = cookies[i].match(/^v2_access_token=(.*)$/);
if (regex_match) {
callback(JSON.parse(decodeURIComponent(regex_match[1])));
return;
}
}
callback(null);
callback(BountysourceServer.get_access_token_from_cookie(cookie));
} else if (BountysourceServer.browser === 'safari') {
callback(BountysourceServer.get_access_token_from_cookie(localStorage['safari_cookie'] || null));
}
},

Expand Down Expand Up @@ -261,6 +293,17 @@ var BountysourceServer = {
});
xhr.open('GET', path, true);
xhr.send();
},

get_access_token_from_cookie: function(cookie) {
var cookies = (cookie || '').split('; ');
for (var i=0; i < cookies.length; i++) {
var regex_match = cookies[i].match(/^v2_access_token=(.*)$/);
if (regex_match) {
return JSON.parse(decodeURIComponent(regex_match[1]));
}
}
return null;
}
};

Expand Down