Skip to content

Commit

Permalink
Basics working
Browse files Browse the repository at this point in the history
  • Loading branch information
hay committed Dec 6, 2017
1 parent 37c343c commit 46985cd
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 21 deletions.
8 changes: 8 additions & 0 deletions background.html
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
</head>
<body>
<script type="module" src="background.js"></script>
</body>
</html>
84 changes: 76 additions & 8 deletions background.js
@@ -1,14 +1,14 @@
var sites = null;
var log = console.log.bind(console);
const log = console.log.bind(console);
const hosts = {};
const queries = {};

function getFilterPath(url) {
var url = new URL(url);
const hostname = url.hostname.replace('www.', '');
return sites[hostname] ? `scripts/${sites[hostname]}.js` : false;
function getHostname(url) {
url = new URL(url);
return url.hostname.replace('www.', '');
}

// Why doesn't Promise have this natively?
Promise.series = function(series) {
function runPromises(series) {
return series.reduce((p, fn) => p.then(fn), Promise.resolve());
};

Expand All @@ -20,7 +20,7 @@ function executeScripts(tabId, scripts) {
});
});

Promise.series(scripts).then(() => log('Loaded all scripts'));
runPromises(scripts).then(() => log('Loaded all scripts'));
}

function handleUpdate(tabId, changeInfo, tab) {
Expand All @@ -43,13 +43,81 @@ function handleUpdate(tabId, changeInfo, tab) {
]);
}

function getScriptsInDirectory(directory, callback) {
chrome.runtime.getPackageDirectoryEntry((entry) => {
entry.getDirectory(directory, { create : false }, (dir) => {
const reader = dir.createReader();
reader.readEntries((results) => {
results = results.map(entry => `./${directory}/${entry.name}`);
callback(results);
});
});
})
}

function executeModuleInTab(tabId, module) {
log(`Loading module ${module.src}`);

if (module.css) {
chrome.tabs.insertCSS(tabId, {
code : module.css
});
}
}

function parseModule(module, src) {
// For easy reference later
module.src = src;

if (module.host) {
hosts[module.host] = module;
} else if (module.query) {
queries[module.query] = module;
}
}

function setupInjector() {
chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
if (change.status === 'complete') {
const hostname = getHostname(tab.url);

if (hostname in hosts) {
executeModuleInTab(tabId, hosts[hostname]);
return;
}

for (let query in queries) {
chrome.tabs.executeScript(tabId, {
code : `!!document.querySelector('${query}');`
}, (result) => {
if (result[0]) {
executeModuleInTab(tabId, queries[query])
}
});
}
}
});
}

function main() {
getScriptsInDirectory("scripts2", (scripts) => {
scripts = scripts.map((script) => {
return import(script).then(m => parseModule(m.default, script));
});

Promise.all(scripts).then(() => {
setupInjector();
});
});

/*
var scriptsUrl = chrome.extension.getURL('scripts.json');
fetch(scriptsUrl).then((res) => res.json()).then((scripts) => {
sites = scripts;
chrome.tabs.onUpdated.addListener(handleUpdate);
});
*/
}

main();
8 changes: 0 additions & 8 deletions bower.json

This file was deleted.

2 changes: 0 additions & 2 deletions lib/.gitignore

This file was deleted.

3 changes: 1 addition & 2 deletions manifest.json
Expand Up @@ -11,7 +11,6 @@
"scripts/*.js"
],
"background" : {
"scripts" : ["background.js"],
"persistent" : false
"page" : "background.html"
}
}
6 changes: 5 additions & 1 deletion scripts/revue.js
Expand Up @@ -7,4 +7,8 @@ $("body").append(`<style>
display: flex;
justify-content: center;
}
</style>`);
</style>`);

export default {
'name' : 'revue'
}
6 changes: 6 additions & 0 deletions scripts2/flickr.js
@@ -0,0 +1,6 @@
// Remove Spaceball to allow right-click to download photos
export default {
host : 'flickr.com',

css : `.flickr-logo-container { display: none; }`
};
16 changes: 16 additions & 0 deletions scripts2/medium.js
@@ -0,0 +1,16 @@
// Remove nonsense
export default {
query : 'meta[name="twitter:app:name:iphone"][content="Medium"]',

css : `
.js-stickyFooter,
.metabar--spacer,
.js-postShareWidget {
display: none;
}
.metabar {
position: relative !important;
}
`
};
12 changes: 12 additions & 0 deletions test.js
@@ -0,0 +1,12 @@
function load(filename) {
import(filename).then((test) => {
test = test.default;
console.log(test.name);
test.run();
});
}

[
'./scripts/test.js',
'./scripts/test2.js'
].forEach(load);

0 comments on commit 46985cd

Please sign in to comment.