-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jhubert/key-commands
Use Chrome commands instead of custom key commands
- Loading branch information
Showing
8 changed files
with
167 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,95 @@ | ||
function setItem(key, value) { | ||
try { | ||
window.localStorage.removeItem(key); | ||
window.localStorage.setItem(key, value); | ||
} catch(e) { | ||
/*jslint browser: true, devel: true, maxerr: 50, indent: 2 */ | ||
/*global chrome */ | ||
|
||
} | ||
} | ||
|
||
function getItem(key) { | ||
var value; | ||
try { | ||
value = window.localStorage.getItem(key); | ||
}catch(e) { | ||
value = "false"; | ||
} | ||
return value; | ||
} | ||
|
||
function clearStorage() { | ||
window.localStorage.clear(); | ||
} | ||
|
||
function injectEditor() { | ||
(function () { | ||
"use strict"; | ||
|
||
if (typeof localStorage.warn === "undefined") { | ||
setItem('warn', 'true'); | ||
function setItem(key, value) { | ||
try { | ||
window.localStorage.removeItem(key); | ||
window.localStorage.setItem(key, value); | ||
} catch (ignore) {} | ||
} | ||
|
||
if (typeof localStorage.save === "undefined") { | ||
setItem('save', 'true'); | ||
function getItem(key) { | ||
var value; | ||
try { | ||
value = window.localStorage.getItem(key); | ||
} catch (ignore) {} | ||
return value; | ||
} | ||
|
||
if (typeof localStorage.keycode === "undefined") { | ||
setItem('keycode', 69); | ||
function setupDefaults() { | ||
if (!localStorage.hasOwnProperty('warn')) { | ||
setItem('warn', 'true'); | ||
} | ||
|
||
if (!localStorage.hasOwnProperty('save')) { | ||
setItem('save', 'true'); | ||
} | ||
} | ||
|
||
chrome.tabs.insertCSS(null, {file: "css_editor.css"}); | ||
function injectEditor() { | ||
setupDefaults(); | ||
|
||
var warn = getItem('warn') === "true" ? true : false, | ||
save = getItem('save') === "true" ? true : false, | ||
modify = getItem('modify') === "true" ? true : false, | ||
boxsize = getItem('boxsize'), | ||
code = "LiveCSSEditor({ warn : " + warn + ", save : " + save + ", modify : " + modify + ", boxsize : '" + boxsize + "' });"; | ||
chrome.tabs.insertCSS(null, {file: "css_editor.css"}); | ||
|
||
chrome.tabs.executeScript(null, {file: "css_editor.js"}, function (response) { | ||
chrome.tabs.executeScript(null, {code: code}); | ||
}); | ||
} | ||
var options = { | ||
warn: getItem('warn') === "true", | ||
save: getItem('save') === "true", | ||
modify: getItem('modify') === "true", | ||
boxsize: getItem('boxsize') || '' | ||
}, | ||
code = 'LiveCSSEditor(' + JSON.stringify(options) + ');'; | ||
|
||
function cleanupEditor() { | ||
chrome.tabs.executeScript(null, {file: "remove_editor.js"}); | ||
} | ||
chrome.tabs.executeScript(null, {file: "css_editor.js"}, function () { | ||
chrome.tabs.executeScript(null, {code: code}); | ||
}); | ||
} | ||
|
||
function loadExistingStyles() { | ||
chrome.tabs.executeScript(null, {file: "existing_styles.js"}); | ||
} | ||
function loadExistingStyle(tabId) { | ||
if (getItem('modify') === 'true') { | ||
chrome.browserAction.setBadgeText({ text: "*", tabId: tabId }); | ||
chrome.tabs.executeScript(null, {file: "existing_styles.js"}); | ||
} | ||
} | ||
|
||
chrome.extension.onMessage.addListener( | ||
function(request, sender, sendResponse) { | ||
if (request.settings) { | ||
sendResponse({ setting : request.settings, value : getItem(request.settings) }); | ||
return; | ||
function handleFileSchemeAccess(isAllowedAccess) { | ||
if (isAllowedAccess) { | ||
injectEditor(); | ||
} else { | ||
if (confirm(chrome.i18n.getMessage('errorFileURL'))) { | ||
chrome.tabs.create({ url: 'chrome://extensions/?id=' + chrome.runtime.id }); | ||
} | ||
} | ||
if (request.modify) { | ||
if (getItem('modify') === 'true') { | ||
chrome.browserAction.setBadgeText ( { text: "*", tabId: sender.tab.id } ); | ||
loadExistingStyles(); | ||
} | ||
|
||
chrome.extension.onMessage.addListener( | ||
function (request, sender, sendResponse) { | ||
if (request.modify) { | ||
loadExistingStyle(sender.tab.id); | ||
} | ||
if (request.openExtensions) { | ||
alert('hi'); | ||
} | ||
sendResponse({}); | ||
} | ||
); | ||
|
||
// Called when the user clicks on the browser action. | ||
chrome.browserAction.onClicked.addListener(function (tab) { | ||
var url = tab.url; | ||
|
||
if (url.indexOf('chrome') === 0) { | ||
alert(chrome.i18n.getMessage('errorChromeURL')); | ||
return; | ||
} | ||
if (request.start) { | ||
|
||
if (url.indexOf('file:///') === 0) { | ||
chrome.extension.isAllowedFileSchemeAccess(handleFileSchemeAccess); | ||
} else { | ||
injectEditor(); | ||
} | ||
if (request.stop) { | ||
if (getItem('save') !== "true" && getItem('warn') === "true" && !confirm('Are you sure?')) { | ||
return; | ||
} | ||
cleanupEditor(); | ||
} | ||
sendResponse({}); | ||
} | ||
); | ||
}); | ||
|
||
// Called when the user clicks on the browser action. | ||
chrome.browserAction.onClicked.addListener(function(tab) { | ||
injectEditor(); | ||
}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.