Permalink
Browse files

Added page action for Chrome extension to force rendering of current

document with XV plugin
  • Loading branch information...
1 parent 0143485 commit 7dd02acb30ccc18ad5f357d15ad5249e1cbaa705 @sergeche committed Aug 7, 2012
@@ -0,0 +1,14 @@
+#Mon Jan 31 17:30:44 MSK 2011
+eclipse.preferences.version=1
+org.eclipse.wst.jsdt.core.compiler.doc.comment.support=enabled
+org.eclipse.wst.jsdt.core.compiler.problem.invalidJavadoc=ignore
+org.eclipse.wst.jsdt.core.compiler.problem.invalidJavadocTags=enabled
+org.eclipse.wst.jsdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=disabled
+org.eclipse.wst.jsdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=enabled
+org.eclipse.wst.jsdt.core.compiler.problem.invalidJavadocTagsVisibility=public
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocComments=ignore
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocCommentsVisibility=public
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocTags=ignore
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocTagsOverriding=disabled
+org.eclipse.wst.jsdt.core.compiler.problem.missingJavadocTagsVisibility=public
@@ -0,0 +1,3 @@
+#Thu Jan 27 16:45:24 MSK 2011
+eclipse.preferences.version=1
+org.eclipse.wst.jsdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates/>
@@ -1,4 +1,6 @@
var xsl = null;
+var lastTabId = null;
+
function loadXsl(url){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
@@ -10,16 +12,7 @@ function loadXsl(url){
xhr.send();
}
-function sendRequest(request, response) {
- chrome.tabs.getSelected(null, function(tab) {
- chrome.tabs.sendRequest(tab.id, request, function(r) {
- if (response)
- response(r);
- });
- });
-}
-
-chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
+chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.action) {
case 'xv.get-dnd-feedback':
sendResponse({image: xv_dnd_feedback.draw(request.text)});
@@ -42,7 +35,30 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
case 'xv.store-settings':
xv_settings.setValue(request.name, request.value);
break;
+ case 'xv.show-page-action':
+ chrome.pageAction.show(sender.tab.id);
+ break;
+ case 'xv.hide-page-action':
+ chrome.pageAction.hide(sender.tab.id);
+ break;
+ }
+});
+
+chrome.pageAction.onClicked.addListener(function(tab) {
+ // toggle forced XV display for current url
+ var forcedURLs = JSON.parse(localStorage.getItem('forced_urls') || '[]');
+ if (_.include(forcedURLs, tab.url)) {
+ forcedURLs = _.without(forcedURLs, tab.url);
+ } else {
+ // make sure there’s no bloated list of documents
+ while (forcedURLs.length > 500)
+ forcedURLs.shift();
+
+ forcedURLs.push(tab.url);
}
+
+ localStorage.setItem('forced_urls', JSON.stringify(forcedURLs));
+ chrome.tabs.reload(tab.id);
});
loadXsl('process.xsl');
@@ -21,7 +21,7 @@ xv_settings = {
setValue: function(name, value) {
this._data[name] = value;
- chrome.extension.sendRequest({
+ chrome.extension.sendMessage({
action: 'xv.store-settings',
name: name,
value: value}, dummy);
@@ -58,7 +58,7 @@ xv_dom.getByClass = function(class_name, context) {
var xv_dnd_feedback = {
draw: function(text, fn) {
- chrome.extension.sendRequest({action: 'xv.get-dnd-feedback', text: text}, function(response){
+ chrome.extension.sendMessage({action: 'xv.get-dnd-feedback', text: text}, function(response){
fn(response.image);
});
}
@@ -84,7 +84,7 @@ function handleDndClicks() {
document.addEventListener('click', function(evt) {
if (is_dnd_mode && copy_text) {
- chrome.extension.sendRequest({action: 'xv.copy', text: copy_text}, dummy);
+ chrome.extension.sendMessage({action: 'xv.copy', text: copy_text}, dummy);
evt.preventDefault();
evt.stopPropagation();
}
@@ -96,21 +96,19 @@ function doTransform(data) {
// https://bugs.webkit.org/show_bug.cgi?id=56263
// typeof(window['handleWebKitXMLViewerOnLoadEvent'])
// document.getElementById('webkit-xml-viewer-source-xml')
- console.log('do transform');
-// var source_doc = _doc.documentElement;
- chrome.extension.sendRequest({action: 'xv.get-xsl', filePath: 'process.xsl'},
+ chrome.extension.sendMessage({action: 'xv.get-xsl', filePath: 'process.xsl'},
function(response) {
var xsl_proc = new XSLTProcessor();
xsl_proc.importStylesheet(xv_utils.toXml(response.fileText));
- chrome.extension.sendRequest({action: 'xv.get-settings'}, function(response){
+ chrome.extension.sendMessage({action: 'xv.get-settings'}, function(response){
xv_settings.load(response.data);
xsl_proc.setParameter(null, 'css', chrome.extension.getURL('xv.css'));
xsl_proc.setParameter(null, 'options_url', chrome.extension.getURL('options.html'));
xsl_proc.setParameter(null, 'custom_css', xv_settings.getValue('custom_css', ''));
- var result = xsl_proc.transformToDocument(_doc);
+ var result = xsl_proc.transformToDocument(data);
document.replaceChild(document.adoptNode(result.documentElement), document.documentElement);
xv_dom.setHTMLContext(result);
@@ -152,20 +150,52 @@ function doTransform(data) {
if (!('_doc' in this)) {
this['_doc'] = document;
}
-
+
+function togglePageAction(isEnabled) {
+ chrome.extension.sendMessage({
+ action: isEnabled ? 'xv.show-page-action' : 'xv.hide-page-action'
+ });
+}
+
// this code will be executed twice since original document will be replaced
// with Chrome's XML tree viewer. The real XML doc will have 'interactive' state,
// but replaced doc will have 'complete' state
document.addEventListener('readystatechange', function() {
if (document.readyState == 'complete') {
- if (canTransform())
+ if (canTransform()) {
doTransform(this['_doc']);
- else {
+ togglePageAction(false);
+ } else {
// Chrome 12.x
var el = document.getElementById('webkit-xml-viewer-source-xml');
if (el) {
el.parentNode.removeChild(el);
doTransform(el);
+ togglePageAction(false);
+ } else {
+ // let’s see if current URL is in forced list
+ togglePageAction(true);
+ chrome.extension.sendMessage({action: 'xv.get-settings'}, function(response) {
+ var forcedURLs = JSON.parse(response.data.forced_urls || '[]');
+ if (_.include(forcedURLs, document.URL)) {
+ var xhr = new XMLHttpRequest();
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ if (xhr.status == 200) {
+ try {
+ doTransform(xv_utils.toXml(xhr.responseText));
+ } catch (e) {
+ console.log('XV: Unable to render document: invalid XML');
+ console.error(e);
+ }
+ }
+ }
+ };
+
+ xhr.open("GET", document.URL, true);
+ xhr.send();
+ }
+ });
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -4,10 +4,11 @@
"description" : "Powerful XML viewer for Google Chrome",
"manifest_version": 2,
"background": {
- "scripts": ["dnd_feedback.js", "xv.js", "background.js"]
+ "page": "background.html"
},
"permissions" : [
"tabs",
+ "clipboardWrite",
"http://*/*",
"https://*/*"
],
@@ -22,5 +23,10 @@
"matches": ["http://*/*", "https://*/*", "file://*/*"],
"run_at": "document_start"
}],
- "web_accessible_resources": ["xv.css"]
+ "web_accessible_resources": ["xv.css"],
+
+ "page_action": {
+ "default_icon": "icon-page-action.png",
+ "default_title": "Toggle page rendering with XV plugin"
+ }
}

0 comments on commit 7dd02ac

Please sign in to comment.