Skip to content
This repository has been archived by the owner on Jun 8, 2018. It is now read-only.

Allow custom resource versions from user specific folder #174

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ A [web browser extension](https://decentraleyes.org) that emulates Content Deliv

> **Note:** Decentraleyes is no silver bullet, but it does prevent a lot of websites from making you send these kinds of requests. Ultimately, you can make Decentraleyes block requests for any missing CDN resources, too.

## Custom Resources

You can set a folder path to a custom resource folder in Addon options menu. Place your versions of supported resources, complying with the scheme of bundled resources (without .dec extension). See resources.js paths for currently supported resources and file names.
Example: [YourFolder]/resources/jquery/3.2.1/jquery.min.js

## Roadmap

Now that there's a solid, Mozilla approved, foundation, it's time to move forward. Mobility, extensibility (through support for community-powered resource packages), and usability, will be the main points of attention during this phase.
Expand Down
67 changes: 43 additions & 24 deletions lib/data-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@

var { Cc, Ci } = require('chrome');
var self = require('sdk/self');
var fileIO = require("sdk/io/file");

//noinspection JSUnresolvedFunction
var ioService = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);

/**
* Absolute resource file paths.
* @var {object} files
*/
var files = require('./files');

/**
* Constants
*/

const DELIVERY_NOTICE = '/**\n * Local delivery by Decentraleyes (' + self.version + ').\n */\n\n';
const PATH_SEP = require('sdk/fs/path').sep;

/**
* Variables
Expand All @@ -46,17 +42,39 @@ var resourceData = self.data;
* Public Methods
*/

function getRedirectionURI(targetPath, characterSet, type) {

var data, dataURI, redirectionURI;

data = _loadResource(targetPath);
dataURI = _buildDataURI(type, characterSet, data);
redirectionURI = ioService.newURI(dataURI, null, null);

function getRedirectionURI(targetPath, characterSet, type, userDir) {   
var data, dataURI, redirectionURI;    
var invalidationChar = false;        

data = _loadUserResource(targetPath, userDir);  //First, attempt to load from User Resource Dir    
if (data == "")    {        
data = _loadResource(targetPath); //After, check bundled Resources        
invalidationChar = true;    
}   
dataURI = _buildDataURI(type, characterSet, data, invalidationChar);   
redirectionURI = ioService.newURI(dataURI, null, null);       
return redirectionURI;
}

function _loadUserResource(targetPath, userDir){
if (!userDir || userDir == "") return "";
if (!userDir.endsWith("/") && !userDir.endsWith("\\"))
userDir += PATH_SEP;
var userTargetPath = userDir + targetPath.substring(0, targetPath.length-4);
userTargetPath = userTargetPath.replace(/\//g, PATH_SEP).replace(/\\/g, PATH_SEP);
var text = "";
try {
if (fileIO.exists(userTargetPath)) {
var file = fileIO.open(userTargetPath, "r");
if (!file.closed) {
text = file.read();
file.close();
}
}
} catch (e) {}
return text;
}

/**
* Exports
*/
Expand All @@ -71,25 +89,26 @@ function _loadResource(targetPath) {

var resource;

// Find the result inside a static path index.
if (!files[targetPath]) {
throw new Error('The requested resource is missing.');
}

// Attempt to load resource contents.
return resource = resourceData.load(targetPath);
    try {        
resource = resourceData.load(targetPath);    
}    
catch (e) {        
throw new Error('The requested resource is missing.');    
}        
return resource;
}

function _buildDataURI(type, characterSet, data) {
function _buildDataURI(type, characterSet, data, stripfirst) {

var addNotice, dataURI;

//noinspection JSUnresolvedVariable
addNotice = require('sdk/simple-prefs').prefs.addNotice;
dataURI = 'data:' + type + ';charset=' + characterSet + ',';

// Remove the syntax invalidation character.
data = data.substring(1);
    // Remove the syntax invalidation character for bundled resources.    
if (stripfirst)        
data = data.substring(1);

if (!addNotice) {
dataURI = dataURI + encodeURIComponent(data);
Expand Down
268 changes: 0 additions & 268 deletions lib/files.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var Interceptor = new Class({

// Fetch local data and create a redirection URI.
try {
redirectionURI = dataHandler.getRedirectionURI(target.path, characterSet, target.type);
redirectionURI = dataHandler.getRedirectionURI(target.path, characterSet, target.type, preferences.userContentDir);
} catch (exception) {
return this.handleMissingCandidate(httpChannel);
}
Expand Down
Loading