1
0
mirror of https://github.com/MatMoul/quickdial-webext.git synced 2024-12-23 15:36:12 +00:00

Large change

This commit is contained in:
MatMoul 2017-10-02 21:29:23 +02:00
parent cb544703ad
commit 34f788fdf3
5 changed files with 144 additions and 46 deletions

View File

@ -111,30 +111,87 @@ core.Bookmarks.getItem = function(bookmarkItem, path){ // Return BookmarkItem fr
} }
core.SiteInfos = {} // Siteinfos helper object core.SiteInfos = {} // Siteinfos helper object
core.SiteInfos.loadInfos = function(url, args, callback){ // args: { icon: false; screenshot: false }, callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: {} ) core.SiteInfos.fromTab = function(callback){ // Retrieve infos from current tab. callback( { url, title, icon, screenshot } || error: callback() )
browser.tabs.getCurrent().then(function(tab){
function whaitLoaded(){
browser.tabs.get(tab.id).then(function(tab){
if(tab.status == 'loading') setTimeout(whaitLoaded, 300);
else{
browser.tabs.update(tab.id, {active: true}).then(function(){
setTimeout(function(){
browser.tabs.captureVisibleTab().then(function(img){
browser.tabs.remove(tab.id);
if(callback) callback( { url: tab.url, title: tab.title, icon: tab.favIconUrl, screenshot: img } );
}, function(){
browser.tabs.remove(tab.id);
if(callback) callback();
});
}, 300);
}, function(){
if(callback) callback();
});
}
}, function(){
if(callback) callback();
});
}
setTimeout(whaitLoaded, 300);
}, function(){
if(callback) callback();
});
}
core.SiteInfos.fromNewTab = function(url, callback){ // Retrieve infos from a new tab. callback( { url, title, icon, screenshot } || error: callback() )
browser.tabs.create({url: url, active: false}).then(function(tab){
browser.tabs.update(tab.id, {muted: true}).then();
function whaitLoaded(){
browser.tabs.get(tab.id).then(function(tab){
if(tab.status == 'loading') setTimeout(whaitLoaded, 300);
else{
browser.tabs.update(tab.id, {active: true}).then(function(){
setTimeout(function(){
browser.tabs.captureVisibleTab().then(function(img){
browser.tabs.remove(tab.id);
if(callback) callback( { url: tab.url, title: tab.title, icon: tab.favIconUrl, screenshot: img } );
}, function(){
browser.tabs.remove(tab.id);
if(callback) callback();
});
}, 300);
}, function(){
if(callback) callback();
});
}
}, function(){
if(callback) callback();
});
}
setTimeout(whaitLoaded, 300);
}, function(){
if(callback) callback();
});
}
core.SiteInfos.fromFrame = function(url, callback){ // Retrieve infos from an iframe. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() )
function pageLoaded(){ function pageLoaded(){
if(!iframe) return; if(!iframe) return;
var docTitle = iframe.contentWindow.document.title; var docTitle = iframe.contentWindow.document.title;
var docIcon = null; var docIcon = null;
var docScreenshot = null; var docScreenshot = null;
//title
if(docTitle == '') docTitle = url; if(docTitle == '') docTitle = url;
if(args && args.icon){ //icon
// //screenshot
} var canvas = document.createElement('canvas');
if(args && args.screenshot){ canvas.style.width = previewWidth.toString() + 'px';
var canvas = document.createElement('canvas'); canvas.style.height = previewHeight.toString() + 'px';
canvas.style.width = previewWidth.toString() + 'px'; canvas.width = previewWidth / 2;
canvas.style.height = previewHeight.toString() + 'px'; canvas.height = previewHeight / 2;
canvas.width = previewWidth / 2; var ctx = canvas.getContext('2d');
canvas.height = previewHeight / 2; ctx.clearRect(0, 0, previewWidth, previewHeight);
var ctx = canvas.getContext('2d'); ctx.save();
ctx.clearRect(0, 0, previewWidth, previewHeight); ctx.scale(0.5, 0.5);
ctx.save(); ctx.drawWindow(iframe.contentWindow, 0, 0, previewWidth, previewHeight, 'rgb(255, 255, 255)');
ctx.scale(0.5, 0.5); ctx.restore();
ctx.drawWindow(iframe.contentWindow, 0, 0, previewWidth, previewHeight, 'rgb(255, 255, 255)'); docScreenshot = canvas.toDataURL();
ctx.restore();
docScreenshot = canvas.toDataURL();
}
document.body.removeChild(iframe); document.body.removeChild(iframe);
iframe = null; iframe = null;
@ -149,15 +206,14 @@ core.SiteInfos.loadInfos = function(url, args, callback){ // args: { icon: false
xmlHttp.open('GET', url, true); xmlHttp.open('GET', url, true);
xmlHttp.onload = function(){ xmlHttp.onload = function(){
iframe = document.createElement('iframe'); iframe = document.createElement('iframe');
iframe.referrerPolicy = "unsafe-url";
iframe.width = previewWidth iframe.width = previewWidth
iframe.height = previewHeight iframe.height = previewHeight
iframe.style.position = 'absolute'; iframe.style.position = 'absolute';
iframe.style.visibility = 'hidden'; //iframe.style.visibility = 'hidden';
document.body.appendChild(iframe);
var content = xmlHttp.responseText.replace('<head>', '<head><base href="' + url + '">'); var content = xmlHttp.responseText.replace('<head>', '<head><base href="' + url + '">');
iframe.srcdoc = content;
iframe.onload = function(){ pageLoaded(); } iframe.onload = function(){ pageLoaded(); }
document.body.appendChild(iframe);
iframe.srcdoc = content;
setTimeout(function(){ pageLoaded(); }, 6000); setTimeout(function(){ pageLoaded(); }, 6000);
} }
xmlHttp.onabort = function(){ if(callback) callback(); } xmlHttp.onabort = function(){ if(callback) callback(); }
@ -165,6 +221,10 @@ core.SiteInfos.loadInfos = function(url, args, callback){ // args: { icon: false
xmlHttp.ontimeout = function(){ if(callback) callback(); } xmlHttp.ontimeout = function(){ if(callback) callback(); }
xmlHttp.send(); xmlHttp.send();
} }
core.SiteInfos.fromWS = function(url, callback){ // Retrieve infos from a Web Service. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() )
console.log('Not implemented');
return core.SiteInfos.fromFrame(url, callback);
}
core.GridNodes = {}; // GridNodes helper object core.GridNodes = {}; // GridNodes helper object
core.GridNodes.sync = function(gridNode, rootPath){ // Sync GridNodes with Bookmarks core.GridNodes.sync = function(gridNode, rootPath){ // Sync GridNodes with Bookmarks
@ -237,7 +297,7 @@ core.GridNodes.getNode = function(gridNode, path){ // Return GridNode from RootG
core.GridNodes.refreshNode = function(gridNode, callback){ // Refresh content of a GridNode core.GridNodes.refreshNode = function(gridNode, callback){ // Refresh content of a GridNode
if(gridNode.__isLoading == true) return; if(gridNode.__isLoading == true) return;
gridNode.__isLoading = true; gridNode.__isLoading = true;
core.SiteInfos.loadInfos(gridNode.url, { screenshot: true }, function(infos){ core.SiteInfos.fromFrame(gridNode.url, function(infos){
if(infos){ if(infos){
gridNode.title = infos.title; gridNode.title = infos.title;
gridNode.image = infos.screenshot; gridNode.image = infos.screenshot;
@ -280,27 +340,37 @@ core.GridNodes.setNodeIndex = function(gridNode, index, newIndex, callback){ //
} }
core.GridNodes.capturePage = function(gridNode, callback){ core.GridNodes.capturePage = function(gridNode, callback){
browser.tabs.create({url: gridNode.url, active: false}).then(function(tab){ browser.tabs.create({url: gridNode.url, active: false}).then(function(tab){
setTimeout(function(){ browser.tabs.update(tab.id, {muted: true}).then(function(){}, function(){});
tab.active = true; function whaitLoaded(){
browser.tabs.update(tab.id, {active: true}).then(function(){ browser.tabs.get(tab.id).then(function(tab){
browser.tabs.captureVisibleTab().then(function(img){ if(tab.status == 'loading'){
//gridNode.title = tab.title setTimeout(whaitLoaded, 300);
browser.tabs.remove(tab.id); } else{
gridNode.image = img; browser.tabs.update(tab.id, {active: true}).then(function(){
browser.bookmarks.update(gridNode.id, { setTimeout(function(){
title: gridNode.title browser.tabs.captureVisibleTab().then(function(img){
}).then(function(bookmarkItem){}, function(){}); browser.tabs.remove(tab.id);
core.Settings.save(); gridNode.title = tab.title
if(callback) callback(); gridNode.image = img;
browser.runtime.sendMessage({ command: 'gridNodesSynced'}).then(function(){}, function(){}); browser.bookmarks.update(gridNode.id, {
}, function(){ title: gridNode.title
browser.tabs.remove(tab.id); }).then(function(bookmarkItem){}, function(){});
if(callback) callback(); core.Settings.save();
}); if(callback) callback();
}, function(){ browser.runtime.sendMessage({ command: 'gridNodesSynced'}).then(function(){}, function(){});
if(callback) callback(); }, function(){
}); browser.tabs.remove(tab.id);
}, 4000); if(callback) callback();
});
}, 300);
}, function(){
if(callback) callback();
});
}
}, function(){});
}
setTimeout(whaitLoaded, 300);
}, function(){ }, function(){
if(callback) callback(); if(callback) callback();
}); });

View File

@ -19,8 +19,6 @@
"<all_urls>" "<all_urls>"
], ],
"content_security_policy": "default-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'; script-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'; child-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'; frame-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'; object-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'; style-src * moz-extension: 'self' 'unsafe-inline' 'unsafe-eval'",
"background": { "background": {
"scripts": ["js/background.js"] "scripts": ["js/background.js"]
}, },

Binary file not shown.

9
src/test/test.html Normal file
View File

@ -0,0 +1,9 @@
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src * 'self'; script-src * 'self'; child-src * 'self'; object-src * 'self'">
<script src="test.js"></script>
</head>
<body>
<iframe id="iframe"></iframe>
</body>
</html>

21
src/test/test.js Normal file
View File

@ -0,0 +1,21 @@
var url = 'https://www.matmoul.ch';
var xmlHttp = new XMLHttpRequest();
xmlHttp.timeout = 10000
xmlHttp.open('GET', url, true);
//xmlHttp.setRequestHeader('X-PINGOTHER', 'pingpong');
//xmlHttp.setRequestHeader('Content-Type', 'text/plain');
//xmlHttp.setRequestHeader('Access-Control-Request-Method', '*');
//xmlHttp.setRequestHeader('Origin', url);
//xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xmlHttp.onload = function(){
iframe = document.getElementById('iframe');
iframe.width = 800
iframe.height = 600
iframe.style.position = 'absolute';
//iframe.style.visibility = 'hidden';
var content = xmlHttp.responseText.replace('<head>', '<head><base href="' + url + '">');
//iframe.onload = function(){ pageLoaded(); }
//document.body.appendChild(iframe);
iframe.srcdoc = content;
}
xmlHttp.send();