Advertisement
Guest User

Semiotic

a guest
Nov 15th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @id             semiotic
  3. // @name           Semiotic
  4. // @version        1.0
  5. // @namespace      
  6. // @author        
  7. // @description    
  8. // @include        *8ch.net/*
  9. // @include        *www.google.com/search*
  10. // @include        *boards.4chan*
  11. // @run-at         document-end
  12. // @grant unsafeWindow
  13. // ==/UserScript==
  14.  
  15. /*To-Do:
  16. scrape full size imgs instead of thumbnail
  17. scaled branding image
  18. movable branding image
  19. bug in brand img load, won't draw unless draw delayed?
  20. resizing window messes up button layout
  21. chrome canvas save problems
  22. */
  23.  
  24. function getOffset( el ) {
  25.     var _x = 0; var _y = 0;
  26.     while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
  27.         _x += el.offsetLeft - el.scrollLeft;
  28.         _y += el.offsetTop - el.scrollTop;
  29.         el = el.offsetParent;
  30.     }
  31.     return { top: _y, left: _x };
  32. }
  33. function insertAfter(newNode, referenceNode) {
  34.     referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  35.     newNode.parentElement = referenceNode.parentNode;
  36. }
  37.  
  38. function initEditCanvas(){
  39.     editCanvas = document.createElement("canvas");
  40.     editCanvas.style.zIndex = 2147483647;
  41.     editCanvas.style.backgroundColor = "white";;
  42.     editCanvas.width = 100;
  43.     editCanvas.height = 100;
  44.     editCanvas.style.width = 100;
  45.     editCanvas.style.height = 100;
  46.  
  47.     editContext = editCanvas.getContext("2d");
  48.     firstImg = new Image();
  49.     brandImg = new Image();
  50. }
  51.  
  52. function centerEditPopup(){
  53.     var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
  54.     var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
  55.     width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
  56.     height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
  57.     var left = ((width / 2) - (100 / 2)) + dualScreenLeft;
  58.     var top = ((height / 2) - (100 / 2)) + dualScreenTop;
  59.     editPopup.style.left = left + "px";
  60.     editPopup.style.top = top + "px";
  61. }
  62.  
  63. function initEditPopup(){
  64.     editPopup = document.createElement("div");
  65.     editPopup.style.position = "fixed";
  66.  
  67.     centerEditPopup();
  68.  
  69.     editPopup.style.zIndex = 2147483646;
  70.     editPopup.style.backgroundColor = "gray";
  71.     editPopup.style.visibility = "hidden";
  72.     document.body.appendChild(editPopup);
  73.  
  74.     editPopupImg = document.createElement("img");
  75.     initEditCanvas();
  76.     editPopup.appendChild(editCanvas);
  77.  
  78.     editBrandImg = document.createElement("img");
  79.  
  80.     editInfoContainer = document.createElement("p");
  81.     editPopup.appendChild(editInfoContainer);
  82.  
  83.     editLoad = document.createElement("input");
  84.     editLoad.setAttribute("type", "file");
  85.     editLoad.setAttribute("value", "LOAD BRANDING IMAGE");
  86.  
  87.     editInfoContainer.appendChild(editLoad);
  88.  
  89.     editCloseButton = document.createElement("input");
  90.     editCloseButton.setAttribute("type", "button");
  91.     editCloseButton.setAttribute("value", "X");
  92.     editCloseButton.setAttribute("name", "closeEditPopupButton");
  93.     editCloseButton.setAttribute("onclick", "hideEditPopup()");
  94.    
  95.     editInfoContainer.appendChild(editCloseButton);
  96. }
  97. initEditPopup();
  98.  
  99. function redrawCanvas(){
  100.     editContext.clearRect ( 0 , 0 , editCanvas.width, editCanvas.height );
  101.     var w = editPopupImg.naturalWidth;
  102.     var h = editPopupImg.naturalHeight;
  103.     editCanvas.width = w;
  104.     editCanvas.height = h;
  105.  
  106.     firstImg.src = editPopupImg.src;
  107.     editContext.drawImage(firstImg, 0,0, w, h);
  108.    
  109.     var brandSize = 75;
  110.     var xA = editCanvas.width - brandSize;
  111.     var yA = editCanvas.height - brandSize;
  112.     var xB = brandSize;
  113.     var yB = brandSize;
  114.  
  115.     setTimeout(function(){ // won't draw otherwise?
  116.     editContext.drawImage(brandImg, xA, yA, xB, yB);
  117.     }
  118.            ,250);
  119. }
  120.  
  121. editLoad.onchange =
  122. function brandImage(evt) {
  123.     var tgt = evt.target || window.event.srcElement,
  124.         files = tgt.files;
  125.  
  126.     if (FileReader && files && files.length) {
  127.         var fr = new FileReader();
  128.         fr.onload = function () {
  129.             editBrandImg.src = fr.result;
  130.         brandImg.src = editBrandImg.src;
  131.  
  132.         redrawCanvas();
  133.  
  134.         }
  135.         fr.readAsDataURL(files[0]);
  136.     } else { alert("File reader not supported on this browser."); }
  137. }
  138.  
  139. function hideEditPopup() { editPopup.style.visibility = "hidden"; }
  140.  
  141. function sEditImage(imgID){
  142.     sImg = document.getElementById("i"+imgID);
  143.    
  144.     var newImg =  sImg;
  145.     editPopup.style.visibility = "visible";
  146.     editPopupImg = newImg;
  147.  
  148.     centerEditPopup();
  149.     redrawCanvas();
  150. }
  151.  
  152. function sTagImages() {
  153.     var imgs = document.getElementsByTagName("img");
  154.  
  155.     for (var x = 0; x < imgs.length; x++) {
  156.     img = imgs[x];
  157.     img.id = "i"+x;
  158.  
  159.     mB = document.createElement("input");
  160.     mB.id = x;
  161.     mB.setAttribute("type", "button");
  162.     mB.setAttribute("value", "Semiotic");
  163.     mB.setAttribute("name", "semioticTagButton");
  164.     mB.setAttribute("onclick", "sEditImage(this.id)");
  165.     mB.zIndex = 2147483647;
  166.  
  167.     var offset = getOffset(img);
  168.     mB.style.position = "absolute";
  169.     mB.style.left = offset.left + "px";
  170.     mB.style.top = (offset.top - 20) + "px";
  171.     mB.style.zIndex = 2147483600;
  172.     document.body.appendChild(mB);
  173.     }
  174. }
  175. sTagImages();
  176.  
  177. unsafeWindow.sEditImage = sEditImage;
  178. unsafeWindow.hideEditPopup = hideEditPopup;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement