Skip to content

Commit

Permalink
Add Firefox support
Browse files Browse the repository at this point in the history
Signed-off-by: Alan D. Tse <alandtse@gmail.com>
  • Loading branch information
alandtse committed Aug 14, 2018
1 parent ed0c0c6 commit 405e620
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 54 deletions.
3 changes: 1 addition & 2 deletions app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@
"open_in_tab": false
},
"web_accessible_resources": [ "license-list/*"],
"permissions": ["storage", "unlimitedStorage", "activeTab"],
"content_security_policy": "script-src 'self' https://spdx.org; object-src 'self'"
"permissions": ["storage", "unlimitedStorage", "activeTab", "*://spdx.org/*"]
}
111 changes: 61 additions & 50 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Enable chromereload by uncommenting this line:
if(process.env.NODE_ENV === 'development'){
if(process.env.NODE_ENV === 'development' && typeof browser === "undefined"){
require('chromereload/devonly')
}

Expand Down Expand Up @@ -27,88 +27,86 @@ chrome.browserAction.setBadgeText({
text: `Diff`
})

chrome.browserAction.onClicked.addListener(function(tab) {
// Send a message to the active tab

function handleClick(tab) {
// Send a message to the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tab) {
var activeTab = tab[0];
activeTabId = activeTab.id
if (! status[activeTabId]){
chrome.tabs.insertCSS(activeTabId, {file:"styles/contentscript.css"});
chrome.tabs.executeScript(activeTabId,{file: "scripts/contentscript.js"},
chrome.tabs.insertCSS(activeTabId, {file:"/styles/contentscript.css"});
chrome.tabs.executeScript(activeTabId,{file: "/scripts/contentscript.js"},
function(result){
chrome.tabs.sendMessage(activeTabId, {"command": "clicked_browser_action"});
});
}else{
chrome.tabs.sendMessage(activeTabId, {"command": "clicked_browser_action"});
}
});
});

chrome.runtime.onStartup.addListener(restore_options());
}

chrome.tabs.onActivated.addListener(function(activeinfo) {
function handleActivate(activeinfo) {
// Set the active tab
activeTabId = activeinfo.tabId;
//console.log("ActiveTabId changed", activeTabId)
});
chrome.windows.onFocusChanged.addListener(function(windowid) {
}

function handleFocusChanged(windowid) {
// Set the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(queryinfo) {
if (queryinfo.length > 0)
activeTabId = queryinfo[0].id;
//console.log("ActiveTabId changed", activeTabId)
});
});
}

//This function responds to changes to storage
chrome.storage.onChanged.addListener(function(changes, area) {
function handleStorageChange(changes, area) {
if (area == "local" && "options" in changes) {
console.log("Detected changed options; reloading")
restore_options();
}
});
}

//respond to content script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.command) {
case "focused":
activeTabId = sender.tab.id;
console.log ("activeTabId", activeTabId)
break;
case "updatelicenselist":
updateList()
break;
case "compareselection":
selection = request.selection
activeTabId = sender.tab.id
if (updating){
pendingcompare = true
comparequeue.push({'selection':selection,'tabId':activeTabId});
console.log("Update pending; queing compare for tab %s; %s queued", activeTabId, comparequeue.length);
status[activeTabId] = "Pending"
break;
}
console.log("tab %s: Starting compare: %s", activeTabId, selection.substring(0,25));
status[activeTabId] = "Comparing"
compareSelection(selection, activeTabId)
break;
case "generateDiff":
activeTabId = sender.tab.id
request["tabId"] = activeTabId;
console.log("tab %s: Generating diff:", activeTabId, request);
status[activeTabId] = "Diffing"
diffcount[activeTabId] = diffcount[activeTabId] + 1
dowork(request);
break;
default:
// console.log("Proxying to worker", request);
// chrome.tabs.sendMessage(activeTab.id, request);
function handleMessage(request, sender, sendResponse) {
switch (request.command) {
case "focused":
activeTabId = sender.tab.id;
console.log ("activeTabId", activeTabId)
break;
case "updatelicenselist":
updateList()
break;
case "compareselection":
selection = request.selection
activeTabId = sender.tab.id
if (updating){
pendingcompare = true
comparequeue.push({'selection':selection,'tabId':activeTabId});
console.log("Update pending; queing compare for tab %s; %s queued", activeTabId, comparequeue.length);
status[activeTabId] = "Pending"
break;
}
console.log("tab %s: Starting compare: %s", activeTabId, selection.substring(0,25));
status[activeTabId] = "Comparing"
compareSelection(selection, activeTabId)
break;
case "generateDiff":
activeTabId = sender.tab.id
request["tabId"] = activeTabId;
console.log("tab %s: Generating diff:", activeTabId, request);
status[activeTabId] = "Diffing"
diffcount[activeTabId] = diffcount[activeTabId] + 1
dowork(request);
break;
default:
// console.log("Proxying to worker", request);
// chrome.tabs.sendMessage(activeTab.id, request);
break;
}
);
}

// Workerqueue functions
// These functions are for allowing multiple workers.
function workeronmessage(event) {
Expand Down Expand Up @@ -436,3 +434,16 @@ function workerdone(id){
workers[id][1] = false
runningworkers--
}

function init() {
console.log("Initializing spdx-diff")
chrome.runtime.onMessage.addListener(handleMessage);
chrome.browserAction.onClicked.addListener(handleClick);
chrome.tabs.onActivated.addListener(handleActivate);
chrome.windows.onFocusChanged.addListener(handleFocusChanged);
chrome.storage.onChanged.addListener(handleStorageChange);
restore_options();
}

chrome.runtime.onStartup.addListener(init);
init();
6 changes: 5 additions & 1 deletion app/scripts/contentscript.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
// Enable chromereload by uncommenting this line:
if(process.env.NODE_ENV === 'development'){
if(process.env.NODE_ENV === 'development' && typeof browser === "undefined"){
require('chromereload/devonly')
}

Expand Down Expand Up @@ -96,6 +96,7 @@ chrome.runtime.onMessage.addListener(
break;
default:
break;
return true;
}
});

Expand Down Expand Up @@ -290,3 +291,6 @@ function restore_options() {
}
});
}

console.log("Spdx-diff ContentScript loaded")
true;
2 changes: 1 addition & 1 deletion app/scripts/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
// Enable chromereload by uncommenting this line:
if(process.env.NODE_ENV === 'development'){
if(process.env.NODE_ENV === 'development' && typeof browser === "undefined"){
require('chromereload/devonly')
}

Expand Down

0 comments on commit 405e620

Please # to comment.