From e7ccd5241a00629588360d8d68ab5628dd0a2c7a Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 12:17:17 +0100 Subject: [PATCH 1/8] Update where message is sent from --- js/background.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/js/background.js b/js/background.js index 992ea1f..709d50f 100644 --- a/js/background.js +++ b/js/background.js @@ -3,7 +3,7 @@ * * This only does anything when the bookmarking button is clicked */ -chromeOrBrowser().browserAction.onClicked.addListener(function(tab) { +chromeOrBrowser().browserAction.onClicked.addListener(function(currentTab) { // If no institution has been set, go to the options page to set one getActiveTenant(function (tenantCode) { if (!tenantCode) { @@ -18,12 +18,8 @@ chromeOrBrowser().browserAction.onClicked.addListener(function(tab) { chromeOrBrowser().tabs.executeScript(null, { file: "/js/bookmarker.js", runAt: 'document_end' + }, function () { + chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode}); }); - - chromeOrBrowser().tabs.query({active: true, currentWindow: true}, function(tabs) { - chromeOrBrowser().tabs.sendMessage(tabs[0].id, {tenantCode: tenantCode}); - return true; - }); - return true; }); }); \ No newline at end of file From 6f2ee2c5f87a0e24a38a0e7722f8ce79c7714219 Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 12:45:13 +0100 Subject: [PATCH 2/8] Use a content script maybe --- js/background.js | 8 ++------ js/bookmarker.js | 47 +++++++++++++++++++++++------------------------ manifest.json | 10 +++++++++- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/js/background.js b/js/background.js index 709d50f..1f331cd 100644 --- a/js/background.js +++ b/js/background.js @@ -15,11 +15,7 @@ chromeOrBrowser().browserAction.onClicked.addListener(function(currentTab) { } } - chromeOrBrowser().tabs.executeScript(null, { - file: "/js/bookmarker.js", - runAt: 'document_end' - }, function () { - chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode}); - }); + console.log(new Date().getTime() + ': background.js: already have bookmarker.js SENDING'); + chromeOrBrowser().tabs.sendMessage(currentTab.id, {tenantCode: tenantCode}); }); }); \ No newline at end of file diff --git a/js/bookmarker.js b/js/bookmarker.js index f34a841..ac971bf 100644 --- a/js/bookmarker.js +++ b/js/bookmarker.js @@ -2,29 +2,28 @@ * Content script to inject the dynamic bookmarking JS directly into the page. */ -(function() { - function chromeOrBrowser() { - return this.browser || chrome; - } - /** - * Injected into page to redirect to the dynamic page parser - * - * @param {String} tenantCode - */ - function bookmarkPage(tenantCode, cb) { - 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' - ); +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) { + 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' + ); + if (document.body) { document.body.appendChild(bookmarker); - cb(); } - chromeOrBrowser().runtime.onMessage.addListener(function(message, sender, sendResponse) { - bookmarkPage(message.tenantCode, function () { - sendResponse({status: 'ok'}); - }); - }); -})(); \ No newline at end of file +} +chromeOrBrowser().runtime.onMessage.addListener(function(message) { + console.log(new Date().getTime() + ': bookmarker.js: onMessage callback'); + bookmarkPage(message.tenantCode); +}); \ No newline at end of file diff --git a/manifest.json b/manifest.json index e7b47e8..d814ff8 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ "persistent": false }, "permissions": [ - "activeTab", "storage", "https://talis-public.talis.com/*" + "activeTab", "storage", "https://talis-public.talis.com/*", "*://*/*" ], "options_ui": { "page": "options.html", @@ -33,6 +33,14 @@ "40": "images/icon40.png" } }, + "content_scripts": [ + { + "matches": [ + "" + ], + "js": ["js/bookmarker.js"] + } + ], "default_locale": "en", "manifest_version": 2 } \ No newline at end of file From 7615c9d80f77bdb4a86558425d49c5d1f8ea6be0 Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 17:36:22 +0100 Subject: [PATCH 3/8] Updated controls on when to execute script --- .gitignore | 2 +- js/background.js | 15 ++++++++++++--- js/bookmarker.js | 8 ++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f9ce37f..d312556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +.idea bower_components/ dist/ node_modules/ diff --git a/js/background.js b/js/background.js index 1f331cd..4042d6d 100644 --- a/js/background.js +++ b/js/background.js @@ -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) { @@ -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}); + } }); }); \ No newline at end of file diff --git a/js/bookmarker.js b/js/bookmarker.js index ac971bf..c0adf04 100644 --- a/js/bookmarker.js +++ b/js/bookmarker.js @@ -5,13 +5,16 @@ 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', @@ -19,11 +22,12 @@ function bookmarkPage(tenantCode) { 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); }); \ No newline at end of file From 5fee9dac72031dd2bee3266a42df5f1f28aab70e Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 17:39:12 +0100 Subject: [PATCH 4/8] Changed manifest back --- manifest.json | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/manifest.json b/manifest.json index d814ff8..e7b47e8 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ "persistent": false }, "permissions": [ - "activeTab", "storage", "https://talis-public.talis.com/*", "*://*/*" + "activeTab", "storage", "https://talis-public.talis.com/*" ], "options_ui": { "page": "options.html", @@ -33,14 +33,6 @@ "40": "images/icon40.png" } }, - "content_scripts": [ - { - "matches": [ - "" - ], - "js": ["js/bookmarker.js"] - } - ], "default_locale": "en", "manifest_version": 2 } \ No newline at end of file From c7620601921db83b696154772b3e2a891d6cb3e8 Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 17:43:13 +0100 Subject: [PATCH 5/8] Revert changes back --- js/bookmarker.js | 53 +++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/js/bookmarker.js b/js/bookmarker.js index c0adf04..11712e7 100644 --- a/js/bookmarker.js +++ b/js/bookmarker.js @@ -1,33 +1,36 @@ /* * Content script to inject the dynamic bookmarking JS directly into the page. */ - -function chromeOrBrowser() { - return this.browser || chrome; -} -/** - * Injected into page to redirect to the dynamic page parser - * - * @param {String} tenantCode - */ -function bookmarkPage(tenantCode) { - if (document.getElementById('bookmarkingScript')) { - document.getElementById('bookmarkingScript').remove(); +(function() { + function chromeOrBrowser() { + return this.browser || chrome; } - 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' + /** + * 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'); + bookmarker.setAttribute('id', 'bookmarkingScript'); - if (document.body) { - document.body.appendChild(bookmarker); + if (document.body) { + document.body.appendChild(bookmarker); + } } -} -chromeOrBrowser().runtime.onMessage.addListener(function(message) { - bookmarkPage(message.tenantCode); -}); \ No newline at end of file + + chromeOrBrowser().runtime.onMessage.addListener(function (message) { + bookmarkPage(message.tenantCode); + }); +})(); \ No newline at end of file From 004dbc285b6c9faa6db30d6c012f007b172b0184 Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 19:23:38 +0100 Subject: [PATCH 6/8] Make this work in chrome AND firefox --- js/background.js | 17 +++++------------ js/options.js | 3 +++ options.html | 1 + 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/js/background.js b/js/background.js index 4042d6d..658310f 100644 --- a/js/background.js +++ b/js/background.js @@ -3,28 +3,21 @@ * * 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) { if (!tenantCode) { - if (window.confirm(chromeOrBrowser().i18n.getMessage('noTenantAlert'))) { - // browser.runtime.openOptionsPage() not supported by ms-edge, so here's a workaround - chromeOrBrowser().tabs.create({ - url: chromeOrBrowser().extension.getURL("options.html") - }); - } - } - if (!executedScript) { + // browser.runtime.openOptionsPage() not supported by ms-edge, so here's a workaround + chromeOrBrowser().tabs.create({ + url: chromeOrBrowser().extension.getURL("options.html") + }); + } else { 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}); } }); }); \ No newline at end of file diff --git a/js/options.js b/js/options.js index 3e5cfbb..9caa90e 100644 --- a/js/options.js +++ b/js/options.js @@ -31,6 +31,9 @@ $(function() { function loadTenantList() { getActiveTenant(function(activeTenant) { + if (!activeTenant) { + $('#optionsHelp').html('
' + chromeOrBrowser().i18n.getMessage('noTenantAlert') + '
'); + } $('#tenantCode').val(activeTenant); getTenants(function(tenants) { var matched = false; diff --git a/options.html b/options.html index 994473f..8cd8479 100644 --- a/options.html +++ b/options.html @@ -16,6 +16,7 @@
+
Institution:
From f0aba9326251278b0c43a37579cdadce628ec976 Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Fri, 4 Oct 2019 19:42:47 +0100 Subject: [PATCH 7/8] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index aa9982d..8452161 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "talis-reading-lists-bookmarker", - "version": "0.3.5", + "version": "0.3.6", "description": "A browser extension to bookmark resources to Talis Aspire Reading Lists", "devDependencies": { "grunt": "^1.0.1", From 011d4f8d471f222964b2faebb3f502707db102ff Mon Sep 17 00:00:00 2001 From: Richard Gubby Date: Mon, 7 Oct 2019 09:58:07 +0100 Subject: [PATCH 8/8] Make ID unique --- js/bookmarker.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js/bookmarker.js b/js/bookmarker.js index 11712e7..ad8f86a 100644 --- a/js/bookmarker.js +++ b/js/bookmarker.js @@ -12,8 +12,10 @@ * @param {String} tenantCode */ function bookmarkPage(tenantCode) { - if (document.getElementById('bookmarkingScript')) { - document.getElementById('bookmarkingScript').remove(); + var bookmarkingScriptId = '__talis_' + tenantCode + '_bookmarkingScript'; + + if (document.getElementById(bookmarkingScriptId)) { + document.getElementById(bookmarkingScriptId).remove(); } var bookmarker = document.createElement('script'); @@ -23,7 +25,7 @@ encodeURIComponent(encodeURI(window.location.href)) + '&bookmarkVersion=1&title=' + encodeURIComponent(document.title) + '&hasJquery=no' ); - bookmarker.setAttribute('id', 'bookmarkingScript'); + bookmarker.setAttribute('id', bookmarkingScriptId); if (document.body) { document.body.appendChild(bookmarker);