Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.
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
2 changes: 2 additions & 0 deletions background.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<!doctype html>
<html>
<script src="lib/require.min.js"></script>
<script src="lib/requireConfig.js"></script>
<script src="background.js"></script>
</html>
52 changes: 30 additions & 22 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, { file: "src/startContentProcessor.js" });
});
requirejs.config(requirejsConfig);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's much more normal to include this call in the config file itself I think. Makes it clearer how that other file is used, and stops this file depending on extra globals existing.


// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
// That fires when a page's URL contains a 'g' ...
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'workflowy.com', schemes: ['https', 'http'] },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
requirejs(
{ baseUrl: chrome.extension.getURL("/") },
["src/startContentProcessor"],
function (Processor) {
chrome.pageAction.onClicked.addListener(function(tab) {
Processor();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a verb. startContentProcessor() (as in the filename) would be clearer.

});

// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
// That fires when a page's URL contains a 'g' ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this comment's wrong.

conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'workflowy.com', schemes: ['https', 'http'] },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
}
);
11 changes: 7 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
frameworks: ['jasmine', 'requirejs'],


// list of files / patterns to load in the browser
files: [
'lib/*.js',
'src/*.js',
'test/*.test.js'
'lib/jquery-2.1.4.min.js',
'test/test-main.js',
{pattern: 'src/*.js', included: false},
{pattern: 'lib/*.js', included: false},
{pattern: 'test/*.test.js', included: false},
{pattern: 'test/mock/*.mock.js', included: false}
],


Expand Down
37 changes: 37 additions & 0 deletions lib/require.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/requireConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var requirejsConfig = {
baseUrl: '/src',
paths: {
lib: '/lib'
}
};
15 changes: 15 additions & 0 deletions lib/requireContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function() {
var global = this;
require.load = function (context, moduleName, url) {
var xhr;
xhr = new XMLHttpRequest();
xhr.open("GET", chrome.extension.getURL(url) + '?r=' + new Date().getTime(), true);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4 && xhr.status === 200) {
eval.call(global, xhr.responseText + '\n//@ sourceURL=' + url);
context.completeLoad(moduleName)
}
};
xhr.send(null);
};
})();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird. Why is this?

The ?r=date part can be done just in require config I'd expect (set urlArgs to "r=" + Date.now()).

It looks like you're changing how require.load works to use AJAX instead of it's built-in mechanisms. That will work fine but it's pretty weird and should be unnecessary; normal loading should work as-is, even in a Chrome extension. Happy to stick with it if you've got a good reason, but it'd be nice to document that reason here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, I copied this from somewhere sorry... Sam C and I were googling around how to add require JS to a chrome extension, it seemed that something along these lines was necessary but I didn't quite figure out why :/

Found it - https://prezi.com/rodnyr5awftr/requirejs-in-chrome-extensions/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. I see: this is so that RequireJS works inside content scripts. Now I look closer, I can see that it only ever gets loaded inside the content scripts, and never in the normal app (where it won't work, because eval isn't allowed by default).

That makes sense, as is fine, but we should document it. I'd stick a comment in here referencing that link and explaining that this is needed because the normal requirejs loading mechanism doesn't work in content scripts, and I'd rename this file to something like "patch-require-for-content-scripts" so it's clearer what's going on at a quick glance. Otherwise this is fine :-)

22 changes: 17 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@
"default_title": "Workflowy2Web"
},
"icons": {
"48": "icons/48.png",
"128": "icons/128.png"
"48": "icons/48.png",
"128": "icons/128.png"
},
"content_scripts": [
{
"matches": [ "https://workflowy.com/*" ],
"js": [ "lib/jquery-2.1.4.min.js", "lib/jszip.min.js", "lib/jszip-utils.min.js", "lib/FileSaver.min.js", "lib/async.min.js", "src/htmlGenerator.js", "src/converter.js", "src/outline.js", "src/utilities.js" ]
"matches": [ "https://workflowy.com/*" ],
"js": [
"lib/require.min.js",
"lib/jquery-2.1.4.min.js",
"lib/requireContent.js",
"lib/requireConfig.js",
"lib/jszip.min.js",
"lib/jszip-utils.min.js",
"lib/FileSaver.min.js",
"lib/async.min.js",
"background.js"
]
}
],
"web_accessible_resources": [
"resources/*"
"resources/*",
"lib/*",
"src/*"
]
}
Loading