Skip to content

Commit

Permalink
Refactor to properly inject through messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinger committed Mar 9, 2018
1 parent 2108e9e commit 0067ca5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 1,556 deletions.
9 changes: 5 additions & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ module.exports = function(grunt) {
{"sizes":"16x16", "src": "images/icon16.png"},
{"sizes": "30x30", "src": "images/icon30.png"},
{"sizes": "50x50", "src": "images/icon50.png"},
{"sizes":"120x120", "src": "images/icon120.png"}
{"sizes":"120x120", "src": "images/icon120.png"},
{"sizes":"176x176", "src": "images/icon176.png"},
];
} else {
delete(manifest['-ms-preload']);
}
manifest.start_url = '';
manifest.scope = '/';
}
grunt.file.write(grunt.config('distdir') + '/manifest.json', JSON.stringify(manifest, null, 2));
});
grunt.registerTask(
Expand Down
1 change: 0 additions & 1 deletion background.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<html>
<script type="text/javascript" src="js/allTenants.js"></script>
<script type="text/javascript" src="js/tenants.js"></script>
<script type="text/javascript" src="js/bookmarker.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</html>
Binary file added images/icon176.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 12 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
*
* This only does anything when the bookmarking button is clicked
*/
chrome.browserAction.onClicked.addListener(function(tab) {
chromeOrBrowser().browserAction.onClicked.addListener(function(tab) {
// If no institution has been set, go to the options page to set one
getActiveTenant(function (tenantCode) {
if (!tenantCode) {
if (window.confirm(chrome.i18n.getMessage('noTenantAlert'))) {
chrome.runtime.openOptionsPage();
if (window.confirm(chromeOrBrowser().i18n.getMessage('noTenantAlert'))) {
chromeOrBrowser().runtime.openOptionsPage();
}
}
// Edge does not support the promise-based APIs (chrome.*), but, conveniently, it also
// requires the bookmarker to be injected via a content_script (whereas other browsers
// seem fine as background scripts), so we'll use that to differentiate
if (this.browser) {
browser.tabs.executeScript(
{ code: 'bookmarkPage("' + tenantCode + '")'}
);
browser.tabs.executeScript(null, {
file: "/js/bookmarker.js"
});

browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
browser.tabs.sendMessage(tabs[0].id, {tenantCode: tenantCode});
});

return;
} else {
// This is effectively the same as bookmarker.js
var bookmarkCode = 'var bookmarker = document.createElement(\'script\');' +
Expand Down
36 changes: 21 additions & 15 deletions js/bookmarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
* Edge needs this to be a content_script that is injected, however.
* @todo Find a way to not duplicate this.
*/
/**
* Injected into page to redirect to the dynamic page parser
*
* @param {String} tenantCode
*/
function bookmarkPage(tenantCode) {
var bookmarker = document.createElement('script');
bookmarker.setAttribute(
'src',
'https://bookmarking.talis.com/' + tenantCode + '/parser?bookmarkButtonVersion=1&uri=' +
encodeURIComponent(encodeURI(window.location.href)) + '&bookmarkVersion=1&title=' +
encodeURIComponent(document.title) + '&hasJquery=no'
);
document.body.appendChild(bookmarker);
}

(function() {
/**
* Injected into page to redirect to the dynamic page parser
*
* @param {String} tenantCode
*/
function bookmarkPage(tenantCode) {
var bookmarker = document.createElement('script');
bookmarker.setAttribute(
'src',
'https://bookmarking.talis.com/' + tenantCode + '/parser?bookmarkButtonVersion=1&uri=' +
encodeURIComponent(encodeURI(window.location.href)) + '&bookmarkVersion=1&title=' +
encodeURIComponent(document.title) + '&hasJquery=no'
);
document.body.appendChild(bookmarker);
}
browser.runtime.onMessage.addListener(function(message) {
bookmarkPage(message.tenantCode);
});
})();
6 changes: 0 additions & 6 deletions js/ms/README.md

This file was deleted.

Loading

0 comments on commit 0067ca5

Please sign in to comment.