I don't know how to wrap it up.

Asked 2 years ago, Updated 2 years ago, 118 views

I'd like to make these codes into one, but I don't know how to do it.

This is the code for the memo function that Chrome extension is considering implementing.
I don't know how to deal with errors, so I always ask questions on various question sites, so I'd like to put them together to make it easier for you to answer.

=================================

manifest.json

{
  "name": "test",
  "manifest_version"—2,
  "version": "1.0",
  "permissions": ["tabs", "storage" ],
  "browser_action": {
    "default_title": "Mister test",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": [
      "background.js"
    ],
    ppersistent :—false
  },
  "content_scripts":[
    {
      "js": ["content_script.js",
      "matches": ["http://*/*", "https://*/*",
      "run_at": " document_end"
    }
  ]
}

popup.js

 "When you launch a pop-up, find out if you saved the memo in that url and pass it to content_script.js"
chrome.tabs.query({active:true, lastFocusedWindow:true},tabs=>{
  chrome.storage.local.get([tabs[0].url], item=>{
    // When you launch a pop-up, look for the url to save the note.
    // If available: Show modal at text location stored in chrome.storage
    if(item[tabs[0].url]){
       chrome.tabs.sendMessage(tabs[0].id, {content:item[tabs[0].url].content,posX:item[tabs[0].url].posX,posY:item[tabs[0].url].posY});
    }
    // If not: Show modal in blank text box in default position
    else{
       chrome.tabs.sendMessage(tabs[0].id, {content:', posX:0, posY:0});
    }
  });
});

background.js

chrome.runtime.onMessage.addListener(content, sender, sendResponse)=>{
  chrome.tabs.query({'active':true,'lastFocusedWindow':true},tabs=>{
    const url=tabs[0].url;//{'active':true, 'lastFocusedWindow':true} tabs (there is only one)
    let data = {};
    // link location and content to a URL
    data[url] = {
      content:content,
      posX: 100,
      posY: 100
    };
    // preservation
     chrome.storage.local.set(data);
  });
  // Reply "I saved it"
   sendResponse('saved');
});

content_script.js

const save=(content)=>{//save(textbox.value)
  chrome.runtime.sendMessage(content,res=>{
      console.log(res);
  });
};
chrome.runtime.onMessage.addListener(message, sender, sendResponse)=>{
  const modalfrm= document.getElementById('ex-memo');
  if(modalfrm!=null)return;
  const modal= document.createElement('div');
  modal.id = 'ex-memo';
  // modal.style.width='500px';
  // modal.style.height='100px';
  modal.style.zIndex='2147483647';
  modal.style.position='fixed';
  modal.style.left=`${message.posX}px`;
  modal.style.top=`${message.posY}px`;
  console.log("modal.style.left:" +modal.style.left);
  console.log("modal.style.top:"+modal.style.top);
  console.log("modal.style.width:" + modal.style.width);
  console.log("modal.style.height:"+modal.style.height);
  const textbox= document.createElement('textarea');
  textbox.style.width='600px';
  textbox.style.height='200px';
  textbox.style.backgroundColor='rgb(67,67,67)';
  textbox.style.color='rgb(51,166,184)';
  textbox.value=message.content;
  modal.appendChild(textbox);
  const saveButton= document.createElement('button');
  saveButton.onclick=()=>save(textbox.value);
  saveButton.innerText='Save';
  modal.appendChild(saveButton);
  document.body.appendChild(modal);
  console.log("function");
  modal.addEventListener ('musedown', mdown);
  modal.addEventListener('touchstart',mdown);
  // "Function when mouse is pressed"
  function mdown(e){
      console.log("mdown");
      This.classList.add("drag");
      // Absorbs differences between touched and mouse events
      if(e.type==="mousedown"){
          var event=e;
      } else{
          var event=e.changedTouches[0];
      }
      // Get relative coordinates in elements
      posX=event.pageX-this.offsetLeft;
      posY = event.pageY-this.offsetTop;
      // Callback to Move Event
      document.body.addEventListener("mousemove", mmove);
      document.body.addEventListener("touchmove", mmove);
  }
  // "Fire when mouse cursor moves"
  function mmove(e){
      console.log("mmove");
      // Get the element you are dragging
     vardrag= document.getElementsByClassName("drag")[0];
      //Similarly absorbs the difference between mouse and touch
      if(e.type==="mousemove"){
          var event=e;
      } else{
          var event=e.changedTouches[0];
      }
      // Suppresses default behavior so that the screen does not move when flicking
      e.preventDefault();
      // Move the element where the mouse moved
      drag.style.top =event.pageY-posY+"px";
      drag.style.left=event.pageX-posX+"px";
      // Fire when mouse button is released or cursor is released
      drag.addEventListener("mouseup",mup);
      document.body.addEventListener("mouseleave",mup);
      drag.addEventListener("touchend",mup);
      document.body.addEventListener("touchleave",mup);

        // "Fire when the mouse button is up"
  function mup(e){
    console.log("mup");
    vardrag= document.getElementsByClassName("drag")[0];
    // Clearing the Move Event Handler
    document.body.removeEventListener("mousemove", mmove);
    drag.removeEventListener("mouseup",mup);
    document.body.removeEventListener("touchmove", mmove);
    drag.removeEventListener("touchend",mup);
    // Also erase class name .drag
    drag.classList.remove("drag");
  }

  }
});

javascript chrome-extension

2022-09-30 19:29

2 Answers

Do you mean you want to create a Chrome extension and put it together for submission to Google?If so, instead of consolidating it into a single HTML file, I will compress and submit it in ZIP format, including icons in the above file group.


2022-09-30 19:29

If you want to put it together just in case you ask a question, on the contrary, you don't need to put it together.The separation of files makes it easier to understand the problem by simply looking at the file name.

If it is to reduce the burden on the respondents, it would be better to try to shorten the code.In other words, you create a toy program that has nothing to do with the nature of the question.If you create as small a program as possible to reproduce the same problem and ask questions based on it, the questions will be easier to read.The Stack Overflow Help Center also has a page titled How to write a short reproducible sample code for your reference.


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.