简体   繁体   中英

Firefox Add-On SDK - Script don't run

So first I did a

jpm init

in a folder dedicated to it.

Then I would like to do basic DOM manipulation with certain page.

That's what I've done :

ff-main.js:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
    include: /\/*.facebook\.*/,
    contentScriptFile: [
        data.url("jquery-2.2.1.min.js"),
        data.url("script.js")
    ]
});

script.js

window.alert('Hey!');

At the moment it is not real DOM manipulation, but it still does not work. It uses RegEx expressions to load only on facebook, with all http/https/www/m + .com .co.uk .nz .fr .de .it .ru (lol).

Then I did basic

jpm xpi
jpm sign ...

When I load page after reboot of Firefox, it does not do anything.

Why?

You are passing a RegExp in the include property of the options object for PageMod . When doing so, your RegExp:

must match the entire URL, not just a subset , and has global, ignoreCase, and multiline disabled.

For more detail, you can see the discussion on MDN . If you have already visited this page, you may need to hit ctrl - F5 , as I just updated some of the RegExp examples.

Your RegExp is /\\/*.facebook\\.*/ . There is no possibility that this will match the entirety of any valid URL. It currently would match things like:

.facebook
Afacebook
//////////////Bfacebook................

But, would not match:

http://www.facebook.com/

Thus, your PageMod will never be applied.

You want something like:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
    include: /[^:/]+:\/\/[^/]*\.facebook\.([^/.]*|co\.uk)\/.*/, 
    contentScriptFile: [
        data.url("jquery-2.2.1.min.js"),
        data.url("script.js")
    ]
});

However, it would be more secure to list the top level domains (TLDs) which you want to match. You have to choose if you want to match too many domains (all TLDs, or all domains containing facebook ), or only match those which you have verified belong to Facebook. Which you choose will depend on what your add-on does, and how you want to balance ease of use for your users vs. security of not running on domains which are close to facebook . Ideally, you would determine all domains which are owned by facebook which show the content you desire and include those.

An example of an include which encompasses the domains you have listed in the question:

    include: /[^:/]+:\/\/[^/]*\.facebook\.(com|co\.uk|nz|fr|de|it|ru)\/.*/,

Complete SDK add-on:

Here is the complete Firefox Add-on SDK extension which I used for testing:

package.json :

{
    "title": "Test PageMod RegExp",
    "name": "pagemodregexp1",
    "version": "0.0.1",
    "description": "Test using a RegExp with PageMod",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}

index.js :

var self = require("sdk/self");
var data = self.data;

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
    //Works (but matches probably bad domain facebook.co.uk):
    //include: /[^:/]+:\/\/[^/]*\.facebook\.([^/.]*|co\.uk)\/.*/,
    //Works (but matches probably bad domain facebook.co.uk):
    //include: /[^:/]+:\/\/[^/]*\.facebook\.(com|co\.uk|nz|fr|de|it|ru)\/.*/,
    //Works:
    include: /[^:/]+:\/\/[^/]*\.facebook\.com\/.*/,


    contentScriptFile: [
//        data.url('jquery-2.2.1.min.js'),
        data.url('contentScript.js')
    ]
});

data/contentScript.js :

window.alert('contentScript.js loaded. URL=' + document.URL);
console.log('contentScript.js loaded. URL=' + document.URL);

Console output when navigating to facebook.com (along with alerts for each console.log() ):

console.log: pagemodregexp1: contentScript.js loaded. URL=https://staticxx.facebook.com/common/referer_frame.php
console.log: pagemodregexp1: contentScript.js loaded. URL=https://staticxx.facebook.com/common/referer_frame.php
console.log: pagemodregexp1: contentScript.js loaded. URL=https://www.facebook.com/

A note on the domains you listed:

facebook.com:   valid
facebook.co.uk: broken good chance it is not be owned by Facebook
                Whois shows registered to: Technomicom Inc.
                NOT registered to Facebook UK LTD (& uses a different physical address)
facebook.nz:    invalid: Server not found
facebook.fr:    valid: redirects to fr-fr.facebook.com
facebook.de:    valid: redirects to de-de.facebook.com
facebook.it:    valid: redirects to facebook.com
facebook.ru:    invalid: Server not found

Given that it appears Facebook redirects all alternate domains to facebook.com , I would suggest not including any other domain in your match other than facebook.com :

include: /[^:/]+:\/\/[^/]*\.facebook\.com\/.*/,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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