<commit_before>// ==UserScript==
// @id             iitc-plugin-draw-tools-sync@viljoviitanen
// @name           IITC plugin: draw tools sync
// @category       Layer
// @version        XXVERSIONXX
// @namespace      https://github.com/viljoviitanen/drawtools-sync-plugin
// @downloadURL    XXURLXX
// @description    Allows syncing draw tools drawings to a server. Server source is available at https://github.com/viljoviitanen/drawtools-sync
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// @grant          none
// ==/UserScript==

// Released under the "ISC" License: https://github.com/viljoviitanen/drawtools-sync-plugin/blob/master/LICENSE

function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};


// PLUGIN START ////////////////////////////////////////////////////////


// use own namespace for plugin
window.plugin.drawToolsSync = function() {};

//XXX TODO make server configurable
window.SYNCSERVER="XXSERVERXX"

window.plugin.drawToolsSync.syncOpt = function() {
      dialog({
        html: '<button onclick="window.open(\''+SYNCSERVER+'/login\')">Login at Sync Server</button><button onclick="window.plugin.drawToolsSync.SyncInit()">Save Drawing</button><button onclick="window.plugin.drawToolsSync.SyncList()">Get List of Drawings</button><button onclick="window.plugin.drawToolsSync.SyncLoadWithKey()">Get with Share Key</button><div id=drawtoolssync_list></div><div id=drawtoolssync_item></div>',
        width: 600,
        dialogClass: 'ui-dialog-drawtoolsSet-copy',
        title: 'Draw Tools Sync'
        });
}

window.plugin.drawToolsSync.SyncDel = function () {
  name=$('#drawtoolssync_name').val()
  key=localStorage['plugin-draw-tools-synckey']
  if (!confirm("Really delete " + window.plugin.drawToolsSync.escapehtml(name) + "?")) return
  $.ajax({
    dataType: "jsonp",
    url: SYNCSERVER+'/delete',
    data: {'key':key},
    xhrFields: { withCredentials: true },
    crossDomain: true,
    success: function(data) {
    if(data.error) {
       alert("Could not delete: "+data.error)
       return
    }
    if(!data.key) {
       alert("Could not delete")
       return
    }

    alert("delete successful")
    //reload
    delete localStorage['plugin-draw-tools-synckey']
    delete localStorage['plugin-draw-tools-syncname']
    window.plugin.drawToolsSync.SyncList()
    }
  })
}

window.plugin.drawToolsSync.SyncSave = function() {
  key=localStorage['plugin-draw-tools-synckey']
  name=$('#drawtoolssync_name').val()
  window.plugin.drawTools.save();
  content=localStorage['plugin-draw-tools-layer']
  shared=$('#drawtoolssync_shared').val()
  //base64-encoded zlib-deflated data.
  c=btoa(zpipe.deflate(JSON.stringify({'key':key,'name':name,'content':content,'shared':shared})))
  //jsonp calls are GET calls and the url must not be greater than about 2k bytes.
  //1888 = max length for c parameter from empirical tests.
  //68 = compressed size of a new empty drawing, empty name and no sharing key
  if(c.length > 1888) {
    alert("Too much data in the drawing! drawing size: "+Math.ceil(100*(c.length-68)/1820)+"%")
    return
  }
  $.ajax({
    dataType: "jsonp",
    url: SYNCSERVER+'/sync',
    data: {'c':c},
    xhrFields: { withCredentials: true },
    crossDomain: true,
    success: function(data) {
    if(data.error) {
       alert("Could not sync data: "+data.error)
       return
    }
    if(!data.content) {
       alert("Could not sync data.")
       return
    }
    if(!data.key) {
       alert("Server did not return the key. What?!")
       return
    }

    // XXX in distant future, check that server returned the same content
    // we put in. if not, someone else modified it betweem saves.
    // provide some options for resolving the conflict.
    alert("save successful. drawing size: "+Math.floor(100*(c.length-68)/1820)+"%")<commit_msg>Tune the drawing size approximation stuff<commit_after>// ==UserScript==
// @id             iitc-plugin-draw-tools-sync@viljoviitanen
// @name           IITC plugin: draw tools sync
// @category       Layer
// @version        XXVERSIONXX
// @namespace      https://github.com/viljoviitanen/drawtools-sync-plugin
// @downloadURL    XXURLXX
// @description    Allows syncing draw tools drawings to a server. Server source is available at https://github.com/viljoviitanen/drawtools-sync
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// @grant          none
// ==/UserScript==

// Released under the "ISC" License: https://github.com/viljoviitanen/drawtools-sync-plugin/blob/master/LICENSE

function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};


// PLUGIN START ////////////////////////////////////////////////////////


// use own namespace for plugin
window.plugin.drawToolsSync = function() {};

//XXX TODO make server configurable
window.SYNCSERVER="XXSERVERXX"

window.plugin.drawToolsSync.syncOpt = function() {
      dialog({
        html: '<button onclick="window.open(\''+SYNCSERVER+'/login\')">Login at Sync Server</button><button onclick="window.plugin.drawToolsSync.SyncInit()">Save Drawing</button><button onclick="window.plugin.drawToolsSync.SyncList()">Get List of Drawings</button><button onclick="window.plugin.drawToolsSync.SyncLoadWithKey()">Get with Share Key</button><div id=drawtoolssync_list></div><div id=drawtoolssync_item></div>',
        width: 600,
        dialogClass: 'ui-dialog-drawtoolsSet-copy',
        title: 'Draw Tools Sync'
        });
}

window.plugin.drawToolsSync.SyncDel = function () {
  name=$('#drawtoolssync_name').val()
  key=localStorage['plugin-draw-tools-synckey']
  if (!confirm("Really delete " + window.plugin.drawToolsSync.escapehtml(name) + "?")) return
  $.ajax({
    dataType: "jsonp",
    url: SYNCSERVER+'/delete',
    data: {'key':key},
    xhrFields: { withCredentials: true },
    crossDomain: true,
    success: function(data) {
    if(data.error) {
       alert("Could not delete: "+data.error)
       return
    }
    if(!data.key) {
       alert("Could not delete")
       return
    }

    alert("delete successful")
    //reload
    delete localStorage['plugin-draw-tools-synckey']
    delete localStorage['plugin-draw-tools-syncname']
    window.plugin.drawToolsSync.SyncList()
    }
  })
}

window.plugin.drawToolsSync.SyncSave = function() {
  key=localStorage['plugin-draw-tools-synckey']
  name=$('#drawtoolssync_name').val()
  window.plugin.drawTools.save();
  content=localStorage['plugin-draw-tools-layer']
  shared=$('#drawtoolssync_shared').val()
  //base64-encoded zlib-deflated data.
  c=btoa(zpipe.deflate(JSON.stringify({'key':key,'name':name,'content':content,'shared':shared})))
  //jsonp calls are GET calls and the url must not be greater than about 2k bytes.
  //1860 = max length for c parameter from empirical tests.
  //68 = compressed size of a new empty drawing, empty name and no sharing key
  //note: with jsonp there is no error message if there is a server error
  //like appengine "414 url too long"
  if(c.length > 1860) {
    alert("-----------------------------------------------------------\n"+
          "Too much data in the drawing, saving will probably fail! Approximate drawing size: "+
          Math.floor(100*(c.length-68)/1792)+"%\n"+
          "-----------------------------------------------------------")
  }
  $.ajax({
    dataType: "jsonp",
    url: SYNCSERVER+'/sync',
    data: {'c':c},
    xhrFields: { withCredentials: true },
    crossDomain: true,
    success: function(data) {
    if(data.error) {
       alert("Could not sync data: "+data.error)
       return
    }
    if(!data.content) {
       alert("Could not sync data.")
       return
    }
    if(!data.key) {
       alert("Server did not return the key. What?!")
       return
    }

    // XXX in distant future, check that server returned the same content
    // we put in. if not, someone else modified it betweem saves.
    // provide some options for resolving the conflict.
    alert("Save successful. Approximate drawing size: "+Math.floor(100*(c.length-68)/1792)+"%")
{'fraction_non_alphanumeric': 0.1696071512585274, 'fraction_numerical': 0.008350976240884498, 'mean_word_length': 2.914825046040516, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 10, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3805542', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2528, 'n_words': 604}