Suddenly, an extension was needed that would push the user from div to div. Began to google, came across an example:

index.js

var buttons = require('sdk/ui/button/action'); var tabs = require("sdk/tabs"); var button = buttons.ActionButton({ id: "mozilla-link", label: "Visit Mozilla", icon: { "16": "./icon-16.png", "32": "./icon-32.png", "64": "./icon-64.png" }, onClick: handleClick }); var data = require("sdk/self").data; function handleClick(state) { tabs.open("http://google.ru"); } 

package.json

 { "title": "Title", "name": "Name", "version": "0.0.1", "description": "Add-on", "main": "index.js", "author": "Author", "engines": { "firefox": ">=38.0a1" }, "license": "MIT", "keywords": [ "jetpack" ] } 

By clicking on the icon opens the site. I am trying to change it so that at the touch of a button there is at least an alert (1). But the error - alert is not defined Tried to connect jQuery, also did not work. How to do all this? Maybe I do not understand something? What is the principle of the extensions? I thought I would quickly write on javascript and ready. And then nothing works for me ..

    1 answer 1

    First, read https://developer.mozilla.org/ru/Add-ons/SDK

    Concerning the same questions:

    I am trying to change it so that at the touch of a button there is at least an alert (1). But the error - alert is not defined

    It does not make sense to use alert in this js file, so it is not defined there.

    Tried to connect jQuery, also did not work.

    Similarly with the case above. This js file does not work directly with the pages, so jQuery is also useless there.

    What is the principle of the extensions?

    The action algorithm in this case will consist of two steps:

    1. By clicking on the button you open the desired page.

    2. After it loads, connect the content scripts , which will contain the working code (alert, jQuery, etc.) for working with this page.

    Code example:

     function handleClick (state) { tabs.open( url: "url.com", onReady: function (tab) { tab.attach( contentScriptFile: './js/content-script.js' ); } ); } 

    In content-script.js: alert("Привет мир");

    • 2
      Thank you!!! - Gikas
    • And how, in fact, to attach to the click on the extension the execution of a function from the file content-script.js? - Gikas
    • @Gikas describe in more detail what you mean by "performing functions on click"? - edem
    • Well, initially, by clicking on the extension, the page of the site opened, and I need to click on a function from the script that I connected to activeTab as contentScriptFileGykess
    • one
      Found such a thing: port.emit("alert", "Message from the add-on"); . I hope correctly, I am doing - Gikas