简体   繁体   English

Firefox 扩展 如何调用 function / 存储任何变量

[英]Firefox Extension How to call function / store any variable

How can i call function of my js script in Firefox extension我如何在 Firefox 扩展中调用我的 js 脚本的 function

{

  "manifest_version": 2,
  "name": "Emailv",
  "version": "1.0",

  "description": "check email is vaild and update in database",

  "icons": {
    "48": "icons/email.png",
    "96": "icons/email.png"
  },

  "content_scripts": [
    {
      "matches": ["*://*.verifyemailaddress.org/*"],
      "js": ["Emailv.js"]
    }
  ]

}

Emaily.js have a function but it Firefox don't getting function and variable Emaily.js 有一个 function 但它 Firefox 没有得到 function 和变量

var t = "free"; 
function keye(){
        var x = document.getElementById("nuuu").value;
        var iop = 109;
        x = x+1;
        //sessionStorage.setItem('key', x);
        var emai = document.querySelector("body > header > form > fieldset >input").value;
    
    }
console.log("start Now");

function fire(){
    document.body.style.border = "5px solid red";
    console.log("start Now");
    document.body.onload = addElement;
    var start = document.querySelector("body > header ");
    start.innerHTML +='<div id="emailv" style="position:relative;margin:auto;width:100px;height:100px;opacity:0.3;z-index:100; background:#000;"><form><input id="nuuu" type="number"><button id="st" onclick="keye()" > Start</button></form><script></div>';
}
window.onload = fire();

I only received start now in console and whenever i try to get var t or try to function it has error Uncaught ReferenceError: keye is not defined and t no defined as it say start now that mean java script run but don't hold any storage for var or function我现在只在控制台中收到 start ,每当我尝试获取 var t 或尝试 function 它有错误 Uncaught ReferenceError: keye is not defined and t no defined 因为它说现在开始这意味着 java 脚本运行但不保存任何存储对于 var 或 function

I add element and add inner HTML and set onclick function and there i run this function.我添加元素并添加内部 HTML 并设置 onclick function 并在那里运行此 ZC1C425268E68384D1AB579。

"content_scripts" default run_at is "document_idle" which means the document and all its resources have finished loading. “content_scripts”默认run_at"document_idle" ,这意味着文档及其所有资源都已完成加载。

window.onload will not fire for your script since script is injected after window 'load' . window.onload不会为您的脚本触发,因为脚本是在window 'load'之后注入的。 It is not needed anyway.无论如何都不需要它。

Here is an example...这是一个例子......

console.log('Starting the code');

const t = "free"; 

// run fire();
fire();

function fire(){
  
  console.log('fire called');
  document.body.style.border = "5px solid red";
  document.body.onload = addElement;
  const start = document.querySelector("body > header ");
  start.innerHTML += '<div id="emailv" style="position:relative;margin:auto;width:100px;height:100px;opacity:0.3;z-index:100; background:#000;"><form><input id="nuuu" type="number"><button id="st"> Start</button></form><script></div>';
}

function keye() {
  
  let x = document.getElementById("nuuu").value;
  const iop = 109;
  x = x+1;
  //sessionStorage.setItem('key', x);
  const emai = document.querySelector("body > header > form > fieldset > input").value;
}

Update on Comment评论更新

Please note that Page JavaScript is isolated from Content JavaScript for security.请注意,出于安全考虑,页面 JavaScript 与内容 JavaScript 是隔离的。 They run in different context/scope.它们在不同的上下文/范围内运行。

When you create a DOM, like <button id="st" onclick="keye()"> Start</button> and inject it into the page DOM, the onclick="keye()" will belong to page JavaScript.当您创建一个 DOM 时,例如<button id="st" onclick="keye()"> Start</button>并将其注入到页面 DOM 中, onclick="keye()"将属于页面 JavaScript。 Obviously, page JavaScript can not access the function keye() which is in the content scope.显然,页面 JavaScript 无法访问内容 scope 中的function keye()

You would need to add the onclick in content scope if you want to run a function in the content scope.如果您想在内容 Z31A1FD140BEA19E18A58D 中运行 function,则需要在内容 scope 中添加onclick

For example:例如:

document.querySelector('#st').addEventListener('click', keye);

Now the 'click' will run keye() in content scope.现在'click'将在内容 scope 中运行keye()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM