Skip to content

320 part2 address erayds comments #380

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

Merged
Merged
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
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ css/options.dist.css: $(LESSC) options/*.less
[ -d css ] || mkdir -p css
$(LESSC) options/options.less css/options.dist.css

js/background.dist.js: $(BROWSERIFY) background.js helpers.js
js/background.dist.js: $(BROWSERIFY) background.js helpers/*.js
[ -d js ] || mkdir -p js
$(BROWSERIFY) -o js/background.dist.js background.js

js/popup.dist.js: $(BROWSERIFY) popup/*.js helpers.js
js/popup.dist.js: $(BROWSERIFY) popup/*.js helpers/*.js
[ -d js ] || mkdir -p js
$(BROWSERIFY) -o js/popup.dist.js popup/popup.js

js/offscreen.dist.js: $(BROWSERIFY) offscreen/*.js
js/offscreen.dist.js: $(BROWSERIFY) offscreen/*.js helpers/*.js
[ -d js ] || mkdir -p js
$(BROWSERIFY) -o js/offscreen.dist.js offscreen/offscreen.js

Expand Down
55 changes: 10 additions & 45 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ require("chrome-extension-async");
const sha1 = require("sha1");
const idb = require("idb");
const BrowserpassURL = require("@browserpass/url");
const helpers = require("./helpers");
const helpers = require("./helpers/base");
const clipboard = require("./helpers/clipboard");

// native application id
var appID = "com.github.browserpass.native";
Expand Down Expand Up @@ -262,15 +263,7 @@ async function copyToClipboard(text, clear = true) {
data: text,
});
} else {
document.addEventListener(
"copy",
function (e) {
e.clipboardData.setData("text/plain", text);
e.preventDefault();
},
{ once: true }
);
document.execCommand("copy");
clipboard.writeToClipboard(text);
}

if (clear) {
Expand Down Expand Up @@ -306,16 +299,7 @@ async function readFromClipboard() {

return response.message;
} else {
const ta = document.createElement("textarea");
// these lines are carefully crafted to make paste work in both Chrome and Firefox
ta.contentEditable = true;
ta.textContent = "";
document.body.appendChild(ta);
ta.select();
document.execCommand("paste");
const content = ta.value;
document.body.removeChild(ta);
return content;
return clipboard.readFromClipboard();
}
}

Expand All @@ -324,7 +308,6 @@ async function readFromClipboard() {
* @since 3.10.0
* @param string path - location of html document to be created
*/
let creatingOffscreen; // A global promise to avoid concurrency issues
async function setupOffscreenDocument(path) {
// Check all windows controlled by the service worker to see if one
// of them is the offscreen document with the given path
Expand All @@ -339,18 +322,11 @@ async function setupOffscreenDocument(path) {
}

// create offscreen document
if (!creatingOffscreen) {
creatingOffscreen = chrome.offscreen.createDocument({
url: path,
reasons: [chrome.offscreen.Reason.CLIPBOARD],
justification: "Read / write text to the clipboard",
});
}

if (creatingOffscreen) {
await creatingOffscreen;
creatingOffscreen = null;
}
await chrome.offscreen.createDocument({
url: path,
reasons: [chrome.offscreen.Reason.CLIPBOARD],
justification: "Read / write text to the clipboard",
});
}

/**
Expand Down Expand Up @@ -630,13 +606,6 @@ async function fillFields(settings, login, fields) {
async function getLocalSettings() {
var settings = helpers.deepCopy(defaultSettings);

try {
// use for debugging only, since dev tools does not show extension storage
await chrome.storage.local.get(console.dir);
} catch (err) {
console.warn("could not display extension local storage");
}

var items = await chrome.storage.local.get(Object.keys(defaultSettings));
for (var key in defaultSettings) {
var value = null;
Expand All @@ -645,11 +614,7 @@ async function getLocalSettings() {
}

if (value !== null && Boolean(value)) {
try {
settings[key] = value;
} catch (err) {
console.error(`getLocalSettings(), error JSON.parse(value):`, err, { key, value });
}
settings[key] = value;
}
}

Expand Down
17 changes: 0 additions & 17 deletions src/helpers.js → src/helpers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,6 @@ function getSetting(key, login, settings) {
*/
function isChrome() {
return chrome.runtime.getURL("/").startsWith("chrom");
/**
* Alternate approach to checking if current browser is chrome or
* chromium based.
*
* @TODO: remove one of these two after probationary period
* to determine which will approach will best suite browserpass
* purposes in the wild.
*
* Above: .getURL("/") will error on "chrome://" protocols
* Below: check user agent, can be altered depending vendor
*/
// const ua = navigator.userAgent;
// const matches = ua.match(/(chrom)/i) || [];
// if (Object.keys(matches).length > 2 && /chrom/i.test(matches[1])) {
// return true;
// }
// return false;
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/helpers/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//------------------------------------- Initialization --------------------------------------//
"use strict";

module.exports = {
readFromClipboard,
writeToClipboard,
};
//----------------------------------- Function definitions ----------------------------------//

/**
* Read plain text from clipboard
*
* @since 3.2.0
*
* @return string The current plaintext content of the clipboard
*/
function readFromClipboard() {
const ta = document.createElement("textarea");
// these lines are carefully crafted to make paste work in both Chrome and Firefox
ta.contentEditable = true;
ta.textContent = "";
document.body.appendChild(ta);
ta.select();
document.execCommand("paste");
const content = ta.value;
document.body.removeChild(ta);
return content;
}

/**
* Copy text to clipboard and optionally clear it from the clipboard after one minute
*
* @since 3.2.0
*
* @param string text Text to copy
* @return void
*/
async function writeToClipboard(text) {
// Error if we received the wrong kind of data.
if (typeof text !== "string") {
throw new TypeError(`Value provided must be a 'string', got '${typeof text}'.`);
}

document.addEventListener(
"copy",
function (e) {
e.clipboardData.setData("text/plain", text);
e.preventDefault();
},
{ once: true }
);
document.execCommand("copy");
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/helpers.ui.js → src/helpers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"use strict";

const m = require("mithril");
const dialog = require("./popup/modalDialog");
const notify = require("./popup/notifications");
const helpers = require("./helpers");
const dialog = require("../popup/modalDialog");
const notify = require("../popup/notifications");
const helpers = require("../helpers/base");

const containsNumbersRegEx = RegExp(/[0-9]/);
const containsSymbolsRegEx = RegExp(/[\p{P}\p{S}]/, "u");
Expand Down
6 changes: 4 additions & 2 deletions src/offscreen/offscreen.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!DOCTYPE html>
<textarea id="text"></textarea>
<script src="../js/offscreen.dist.js"></script>
<html>
<body></body>
<script src="../js/offscreen.dist.js"></script>
</html>
49 changes: 3 additions & 46 deletions src/offscreen/offscreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//------------------------------------- Initialization --------------------------------------//
"use strict";
const clipboard = require("../helpers/clipboard");

//----------------------------------- Function definitions ----------------------------------//
chrome.runtime.onMessage.addListener(handleMessage);
Expand All @@ -20,10 +21,10 @@ async function handleMessage(message, sender, sendResponse) {
try {
switch (message.type) {
case "copy-data-to-clipboard":
writeToClipboard(message.data);
clipboard.writeToClipboard(message.data);
break;
case "read-from-clipboard":
reply = readFromClipboard();
reply = clipboard.readFromClipboard();
break;
default:
console.warn(`Unexpected message type received: '${message.type}'.`);
Expand All @@ -33,47 +34,3 @@ async function handleMessage(message, sender, sendResponse) {
sendResponse({ status: "error", message: e.toString() });
}
}

/**
* Read plain text from clipboard
*
* @since 3.2.0
*
* @return string The current plaintext content of the clipboard
*/
function readFromClipboard() {
const ta = document.querySelector("#text");
// these lines are carefully crafted to make paste work in both Chrome and Firefox
ta.contentEditable = true;
ta.textContent = "";
ta.select();
document.execCommand("paste");
const content = ta.value;
return content;
}

/**
* Copy text to clipboard and optionally clear it from the clipboard after one minute
*
* @since 3.2.0
*
* @param string text Text to copy
* @param boolean clear Whether to clear the clipboard after one minute
* @return void
*/
async function writeToClipboard(text) {
// Error if we received the wrong kind of data.
if (typeof text !== "string") {
throw new TypeError(`Value provided must be a 'string', got '${typeof text}'.`);
}

document.addEventListener(
"copy",
function (e) {
e.clipboardData.setData("text/plain", text);
e.preventDefault();
},
{ once: true }
);
document.execCommand("copy");
}
4 changes: 2 additions & 2 deletions src/popup/addEditInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Settings = require("./models/Settings");
const Tree = require("./models/Tree");
const notify = require("./notifications");
const dialog = require("./modalDialog");
const helpers = require("../helpers");
const helpersUI = require("../helpers.ui");
const helpers = require("../helpers/base");
const helpersUI = require("../helpers/ui");
const layout = require("./layoutInterface");

module.exports = AddEditInterface;
Expand Down
4 changes: 2 additions & 2 deletions src/popup/detailsInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = DetailsInterface;

const m = require("mithril");
const Moment = require("moment");
const helpers = require("../helpers");
const helpersUI = require("../helpers.ui");
const helpers = require("../helpers/base");
const helpersUI = require("../helpers/ui");
const layout = require("./layoutInterface");
const Login = require("./models/Login");
const Settings = require("./models/Settings");
Expand Down
2 changes: 1 addition & 1 deletion src/popup/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Moment = require("moment");
const SearchInterface = require("./searchinterface");
const BrowserpassURL = require("@browserpass/url");
const dialog = require("./modalDialog");
const helpers = require("../helpers");
const helpers = require("../helpers/base");
const layout = require("./layoutInterface");
let overrideDefaultSearchOnce = true;

Expand Down
2 changes: 1 addition & 1 deletion src/popup/modalDialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const m = require("mithril");
const redraw = require("../helpers.redraw");
const redraw = require("../helpers/redraw");

const modalId = "browserpass-modal";
const CANCEL = "Cancel";
Expand Down
4 changes: 2 additions & 2 deletions src/popup/models/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require("chrome-extension-async");
const sha1 = require("sha1");
const helpers = require("../../helpers");
const helpersUI = require("../../helpers.ui");
const helpers = require("../../helpers/base");
const helpersUI = require("../../helpers/ui");
const Settings = require("./Settings");

// Search for one of the secret prefixes
Expand Down
2 changes: 1 addition & 1 deletion src/popup/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const m = require("mithril");
const redraw = require("../helpers.redraw");
const redraw = require("../helpers/redraw");
const uuidPrefix = RegExp(/^([a-z0-9]){8}-/);
const NOTIFY_CLASS = "m-notifications";

Expand Down
2 changes: 1 addition & 1 deletion src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require("chrome-extension-async");
const Login = require("./models/Login");
const Settings = require("./models/Settings");
// utils, libs
const helpers = require("../helpers.ui");
const helpers = require("../helpers/ui");
const m = require("mithril");
// components
const AddEditInterface = require("./addEditInterface");
Expand Down
2 changes: 1 addition & 1 deletion src/popup/searchinterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = SearchInterface;

const BrowserpassURL = require("@browserpass/url");
const dialog = require("./modalDialog");
const helpers = require("../helpers");
const helpers = require("../helpers/base");
const m = require("mithril");

/**
Expand Down