Skip to content

Commit

Permalink
Updated controls on when to execute script
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Gubby committed Oct 4, 2019
1 parent 6f2ee2c commit 7615c9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

.idea
bower_components/
dist/
node_modules/
15 changes: 12 additions & 3 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* This only does anything when the bookmarking button is clicked
*/
var executedScript = false;
chromeOrBrowser().browserAction.onClicked.addListener(function(currentTab) {
// If no institution has been set, go to the options page to set one
getActiveTenant(function (tenantCode) {
Expand All @@ -14,8 +15,16 @@ chromeOrBrowser().browserAction.onClicked.addListener(function(currentTab) {
});
}
}

console.log(new Date().getTime() + ': background.js: already have bookmarker.js SENDING');
chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode});
if (!executedScript) {
chromeOrBrowser().tabs.executeScript(null, {
file: "/js/bookmarker.js",
runAt: 'document_end'
}, function () {
executedScript = true;
chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode});
});
} else {
chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode});
}
});
});
8 changes: 6 additions & 2 deletions js/bookmarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
function chromeOrBrowser() {
return this.browser || chrome;
}
console.log(new Date().getTime() + ': bookmarker.js');
/**
* Injected into page to redirect to the dynamic page parser
*
* @param {String} tenantCode
*/
function bookmarkPage(tenantCode) {
if (document.getElementById('bookmarkingScript')) {
document.getElementById('bookmarkingScript').remove();
}

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'
);
bookmarker.setAttribute('id', 'bookmarkingScript');

if (document.body) {
document.body.appendChild(bookmarker);
}
}
chromeOrBrowser().runtime.onMessage.addListener(function(message) {
console.log(new Date().getTime() + ': bookmarker.js: onMessage callback');
bookmarkPage(message.tenantCode);
});

0 comments on commit 7615c9d

Please sign in to comment.