Advertisement
Ladies_Man

Squire CA

Aug 22nd, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //manifest
  2. {
  3.     "manifest_version": 2,
  4.     "name": "AS CA",
  5.     "version": "1.2",
  6.     "description": "pre-alpha",
  7.     "minimum_chrome_version": "45.0",
  8.     "offline_enabled": true,
  9.     "icons": {
  10.         "16":   "images/icon_new.png",
  11.         "128":  "images/icon_new_128.png"
  12.     },
  13.     "app": {
  14.         "background": {
  15.             "scripts":      ["js/jquery-2.1.4.js", "js/background.js"]
  16.         }
  17.     },
  18.     "externally_connectable": {
  19.         "matches": ["*://localhost:*/"]
  20.     },
  21.     "permissions": [
  22.         "storage",
  23.         "webview",
  24.         "<all_urls>",
  25.         {
  26.             "fileSystem": [
  27.                 "write"
  28.             ]
  29.         }
  30.     ]
  31. }
  32.  
  33.  
  34. //background
  35. chrome.app.runtime.onLaunched.addListener(function() {
  36.     chrome.app.window.create('index.html', {
  37.         id: "messaging1ID",
  38.         outerBounds: {
  39.             minWidth: 404,
  40.             maxWidth: 404,
  41.             width: 380,
  42.            
  43.             minHeight: 400,
  44.             maxHeight: 1000,
  45.             height: 500
  46.         }
  47.     });
  48. });
  49.  
  50. chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
  51.     if ("open_app" == message.key) {
  52.         chrome.app.window.create('index.html', {
  53.             'outerBounds': {
  54.             'minWidth': 360,
  55.             'maxWidth': 360,
  56.             'width': 360,
  57.             'minHeight': 400,
  58.             'height': 500
  59.             }
  60.         });
  61.     } else {
  62.         chrome.app.window.create('index.html', {
  63.             'outerBounds': {
  64.             'minWidth': 10,
  65.             'maxWidth': 10,
  66.             'width': 10,
  67.             'minHeight': 10,
  68.             'height': 10
  69.             }
  70.         });
  71.     }
  72. });
  73.  
  74.  
  75. //index
  76. <!DOCTYPE html>
  77.  
  78. <html>
  79.   <head>
  80.  
  81.     <title>S CA</title>
  82.     <link href="style.css" rel="stylesheet">
  83.     <script src="js/jquery-2.1.4.js"></script>
  84.     <script src="js/index.js"></script>
  85.    
  86.   </head>
  87.   <body>
  88.    
  89.     <div id="refresh">Refresh</div>
  90.     <div id="export">Export
  91.         <div id="status"></div>
  92.     </div>
  93.    
  94.     <script src="js/export.js"></script>
  95.    
  96.   </body>
  97. </html>
  98.  
  99.  
  100. //index
  101. function sleep(milliseconds) {
  102.     var start = new Date().getTime();
  103.     for (var i = 0; i < 1e7; i++) {
  104.         if ((new Date().getTime() - start) > milliseconds)
  105.             break;
  106.     }
  107. }
  108.  
  109. function generate_feed(xml) {
  110.     var items = xml.getElementsByTagName("item");
  111.  
  112.     var doc = document.implementation.createHTMLDocument("New Document");
  113.     var i, mark, el = doc.createElement('html');
  114.     el.innerHTML = "<html><head><title>S CA</title></head><body></body></html>";
  115.    
  116.     mark = 0;
  117.     for (i = 0; i < items.length; i++) {
  118.         var div = document.createElement("div");
  119.         div.className = "block";
  120.                
  121.         //category     
  122.         var inner_cat = document.createElement("div");
  123.         inner_cat.className = "cat";
  124.         inner_cat.innerHTML = $(items[i]).find("category").html();
  125.         div.appendChild(inner_cat);
  126.                
  127.         //date     
  128.         var inner_date = document.createElement("div");
  129.         inner_date.className = "date";
  130.         var pub_date = $(items[i]).find("pubDate").html();
  131.         var date = new Date(pub_date);
  132.         var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  133.         var string = date.getDate() + " " + months[date.getMonth()];
  134.         inner_date.innerHTML = string;
  135.         div.appendChild(inner_date);
  136.              
  137.         //image  
  138.         var inner_img = document.createElement("img");
  139.         inner_img.className = "prev";
  140.         inner_img.id = $(items[i]).find("enclosure").attr('length');
  141.         inner_img.src = 'images/prev_load2_std.GIF';
  142.         inner_img.style.width = "64px";
  143.         inner_img.style.marginLeft = "148px";
  144.         var img_src = $(items[i]).find("enclosure").attr('url');
  145.            
  146.         var xhr = new XMLHttpRequest();
  147.         xhr.open('GET', img_src, true);
  148.         xhr.responseType = 'blob';
  149.         xhr.send();
  150.         xhr.onload = function(e) {
  151.             if (200 == this.status) {
  152.                 inner_img.src = window.URL.createObjectURL(this.response);
  153.                 console.log("XHR[" + mark + "]" + inner_img.src);
  154.                 var id = this.getResponseHeader('Content-Length').toString();
  155.                 document.getElementById(id).src = inner_img.src;
  156.                 document.getElementById(id).style.width = "160px";
  157.                 document.getElementById(id).style.marginLeft = "auto";
  158.                 mark++;
  159.             }
  160.         }
  161.        
  162.         div.appendChild(inner_img);
  163.                
  164.         //title    
  165.         var inner_title = document.createElement("div");
  166.         inner_title.className = "tit";
  167.         inner_title.innerHTML = $(items[i]).find("title").html();
  168.         div.appendChild(inner_title);
  169.                
  170.         //description
  171.         var inner_descr = document.createElement("div");
  172.         inner_descr.className = "dsc";
  173.         inner_descr.innerHTML = $(items[i]).find("description").html();
  174.         div.appendChild(inner_descr);
  175.        
  176.         var inner_br = document.createElement("br");
  177.         inner_descr.appendChild(inner_br);
  178.                
  179.         //link
  180.         var inner_link = document.createElement("a");
  181.         inner_link.className = "lnk";
  182.         inner_link.href = $(items[i]).find("link").html();
  183.         if (-1 != inner_link.href.indexOf("game") || -1 != inner_link.href.indexOf("thrones") || -1 != inner_link.href.indexOf("prestol")) {
  184.             div.style.backgroundColor = "#FF9933";
  185.             inner_img.src = 'images/prev_load2_spec.GIF';
  186.         }
  187.         //inner_link.target = "_blank";
  188.         inner_link.innerHTML = "Read more";
  189.         inner_descr.appendChild(inner_link);
  190.                
  191.                
  192.         el.getElementsByTagName('body')[0].appendChild(div);
  193.     }
  194.     console.log("mark:" + mark);
  195.        
  196.        
  197.     string = JSON.stringify(el.getElementsByTagName("body")[0].innerHTML);
  198.     string = string.replace(/<!--\[CDATA\[/g, '').replace(/\]\]-->/g, '');
  199.     console.log('stringified:' + string);
  200.     return string;
  201. }
  202.  
  203. function set_webview() {
  204.     var full_text_url = this.href;
  205.     chrome.app.window.create("webview.html",
  206.         {
  207.             hidden: true,
  208.             outerBounds: {
  209.                 minWidth: 720,
  210.                 maxWidth: 1024,
  211.                 width: 720,
  212.                 minHeight: 500,
  213.                 maxHeight: 1024,
  214.                 height: 800
  215.             }
  216.         }, function(app_window) {
  217.             app_window.contentWindow.addEventListener('DOMContentLoaded', function() {
  218.                 var wv = app_window.contentWindow.document.querySelector('webview');
  219.                 wv.src = full_text_url;
  220.                
  221.                 wv.addEventListener("loadcommit", function(e) {
  222.                     this.executeScript({
  223.                         code: "",
  224.                         runAt: "document_start"
  225.                     });
  226.                 });
  227.                
  228.                 app_window.show();
  229.             });
  230.         }
  231.     );
  232.     e.stopPropagation();
  233. }
  234.  
  235. function modify_index(response_data) {
  236.     document.getElementsByTagName("body")[0].innerHTML += JSON.parse(response_data);
  237.     var i, h, blocks = document.getElementsByClassName('block');
  238.    
  239.     for (i = 0; i < blocks.length; i++) {
  240.         blocks[i].addEventListener("click", function() {
  241.             if ("visible" == this.getElementsByClassName('dsc')[0].style.visibility) {
  242.                 this.getElementsByClassName('dsc')[0].style.visibility = "hidden";
  243.                 this.getElementsByClassName('date')[0].style.visibility = "hidden";
  244.                 $(this).css("height", "180px");
  245.             } else {
  246.                 this.getElementsByClassName('dsc')[0].style.visibility = "visible";
  247.                 this.getElementsByClassName('date')[0].style.visibility = "visible";
  248.                 $(this).css("height", "auto");
  249.             }
  250.         });
  251.         $(blocks[i]).on("click", ".lnk", set_webview);
  252.     }
  253. }
  254.  
  255. function clear_dom() {
  256.     var blocks = document.getElementsByClassName("block");
  257.     for (var i = 0; i < blocks.length; i++) {
  258.         $('.block').remove();
  259.     }
  260. }
  261.  
  262. function get_rss(rss_url) {
  263.    
  264.     console.log('requseting url:' + rss_url);
  265.    
  266.     var xhr = new XMLHttpRequest();
  267.     xhr.open("GET", rss_url, true);
  268.     xhr.send(null);
  269.     xhr.onreadystatechange = function() {
  270.         if (4 == xhr.readyState) {
  271.             if (200 == xhr.status) {
  272.                 console.log(xhr.responseXML);
  273.                 feed = generate_feed(xhr.responseXML);
  274.                 clear_dom();
  275.                 modify_index(feed);
  276.             } else {
  277.                 console.log('status:' + xhr.status);
  278.             }
  279.         } else {
  280.             console.log('readyState:' + xhr.readyState);
  281.         }
  282.     }
  283.     console.log("_Ret from get_rss()");
  284. }
  285.  
  286. document.addEventListener('DOMContentLoaded', function() {
  287.     $(document).on("click", "#refresh", function() {
  288.         this.innerHTML = "+";
  289.         get_rss("http://lenta.ru/rss");
  290.     });
  291.     //document.getElementById("refresh").click();
  292.     console.log("_Ret from event listener");
  293. });
  294.  
  295.  
  296.  
  297. //export
  298. (function() {
  299.     var text = "";
  300.     var savedFileEntry, fileDisplayPath;   
  301.    
  302.     function get_data_as_text() {
  303.         var rss_url = "http://lenta.ru/rss";
  304.         var xhr = new XMLHttpRequest();
  305.         xhr.open("GET", rss_url, true);
  306.         xhr.send(null);
  307.         xhr.onreadystatechange = function() {
  308.             if (4 == xhr.readyState && 200 == xhr.status) {
  309.                
  310.                 var xml = xhr.responseXML;
  311.                 var items = xml.getElementsByTagName("item");
  312.                 for (var i = 0; i < items.length; i++) {
  313.                     text += $(items[i]).find("title").html() + '\n';
  314.                     text += '\t' + $(items[i]).find("description").html() + '\n';
  315.                     text += '\n';
  316.                     for (j = 0; j < 70; j++) text += "=";
  317.                     text += '\n';
  318.                 }
  319.                
  320.                 text = text.replace(/<!\[CDATA\[/g, '').replace(/\]\]>/g, '');
  321.                 return true;
  322.                
  323.             }
  324.         }
  325.        
  326.         return true;
  327.     }
  328.    
  329.    
  330.     function getTodosAsText(callback) {
  331.         //var text = "lorem ipsum dolor sit amet";
  332.         callback(text);
  333.     }
  334.  
  335.     function exportToFileEntry(fileEntry) {
  336.         savedFileEntry = fileEntry;
  337.  
  338.         var status = document.getElementById('export');
  339.  
  340.         // Use this to get a file path appropriate for displaying
  341.         chrome.fileSystem.getDisplayPath(fileEntry, function(path) {
  342.             fileDisplayPath = path;
  343.             status.innerText = 'Exporting';
  344.         });
  345.  
  346.         getTodosAsText( function(contents) {
  347.  
  348.             fileEntry.createWriter(function(fileWriter) {
  349.  
  350.             var truncated = false;
  351.             var blob = new Blob([contents]);
  352.  
  353.             fileWriter.onwriteend = function(e) {
  354.                 if (!truncated) {
  355.                     truncated = true;
  356.                     // You need to explicitly set the file size to truncate
  357.                     // any content that might have been there before
  358.                     this.truncate(blob.size);
  359.                     return;
  360.                 }
  361.                 status.innerText = 'Export completed';
  362.             };
  363.  
  364.             fileWriter.onerror = function(e) {
  365.                 status.innerText = 'Export failed: '+e.toString();
  366.             };
  367.  
  368.             fileWriter.write(blob);
  369.  
  370.             });
  371.         });
  372.     }
  373.  
  374.     function doExportToDisk() {
  375.         if (savedFileEntry) {
  376.  
  377.             exportToFileEntry(savedFileEntry);
  378.  
  379.         } else {
  380.  
  381.             chrome.fileSystem.chooseEntry( {
  382.                 type: 'saveFile',
  383.                 suggestedName: 'todos.txt',
  384.                 accepts: [ { description: 'Text files (*.txt)',
  385.                        extensions: ['txt']} ],
  386.                 acceptsAllTypes: true
  387.             }, exportToFileEntry);
  388.  
  389.         }
  390.     }
  391.    
  392.     document.getElementById("export").addEventListener("click", function() {
  393.         if (true == get_data_as_text()) {
  394.             doExportToDisk();
  395.         }
  396.     });
  397.    
  398. })();
  399.  
  400.  
  401.  
  402. //webview
  403. <!DOCTYPE html>
  404.  
  405. <html>
  406.     <head>
  407.         <meta charset="utf-8">
  408.        
  409.     </head>
  410.    
  411.     <body>
  412.         <webview style="width: 1024px; height: 800px;"></webview>
  413.    
  414.         <script src="js/webview.js"></script>
  415.     </body>
  416.  
  417. </html>
  418.  
  419.  
  420. //webview??
  421. document.addEventListener('DOMContentLoaded', function() {
  422.     if (-1 != document.URL.indexOf("lenta.ru")) {
  423.         document.getElementById("ban_billboard").style.display = "none";
  424.     }
  425.    
  426. });
  427.  
  428.  
  429. var wv = document.querySelector('webview');
  430. wv.addEventListener('loadcommit', function() {
  431.   wv.insertCSS({
  432.     code: 'body { background: red !important; }',
  433.     runAt: 'document_start'
  434.   });
  435. });
  436.  
  437.  
  438.  
  439. //style
  440. body {
  441.     font-size: 11pt;
  442.     background-color: #808080;
  443.     list-style-type: none;
  444.     -webkit-user-select: none;
  445. }
  446.  
  447. a {
  448.     text-decoration: none;
  449.     color: #FF9933;
  450. }
  451.  
  452. a:hover {
  453.     color: black;
  454. }
  455.  
  456. #refresh , #export{
  457.     display: block;
  458.     cursor: pointer;
  459.     float: left;
  460.     width: 172px;
  461.     text-align: center;
  462.     margin: 5px;
  463.     background-color: #D0D0D0;
  464.     border: 1px solid black;
  465.     border-radius: 2px;
  466. }
  467.  
  468. #refresh:hover, #export:hover {
  469.     background-color: #F0F0F0;
  470. }
  471.  
  472. #export {
  473.     float: right;
  474. }
  475.  
  476. .block {
  477.     display: block;
  478.     float: left;
  479.     cursor: default;
  480.     height: 180px;
  481.     width: 360px;
  482.     margin: 5px;
  483.     background-color: #D0D0D0;
  484. }
  485.  
  486. .cat {
  487.     display: inherit;
  488.     float: left;
  489.     cursor: inherit;
  490.     text-indent: 5px;
  491. }
  492.  
  493. .date {
  494.     display: inherit;
  495.     visibility: hidden;
  496.     margin-right: 5px;
  497.     text-align: right;
  498. }
  499.  
  500. .prev  {
  501.     display: inherit;
  502.     cursor: inherit;
  503.     width: 160px;
  504.     margin-left: auto;
  505.     margin-right: auto;
  506. }
  507.  
  508. .tit {
  509.     display: inherit;
  510.     cursor: inherit;
  511.     height: auto;
  512.     text-align: center;
  513.     padding: 5px;
  514. }
  515.  
  516. .dsc {
  517.     display: inherit;
  518.     cursor: inherit;
  519.     visibility: hidden;
  520.     text-align: justify;
  521.     margin: 5px;
  522.     padding: 5px;
  523.     background-color: #F0F0F0;
  524. }
  525.  
  526. .lnk {
  527.     visibility: inherit;
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement