1

I try to create a WebExtension which do some things if it's on a specific site in a specific folder. I tried to recognice the site and the subfolder like this:

var on = true;

//This function checks if the current site is Google after something was searched
function check_if_engine() {
  var loc = window.location.href;
  if (loc.includes("google")) {
    alert("Includes google");
    if (log.includes("search?")) {
      return true;
    }
    return false;
  }
  return false;

  //...

function start_all() {
  if (on) {
    alert("Addon activated!");
    if (check_if_engine()) {
      alert("Website is Google");
      //...
}
//Checks if the tab loaded a new URL
browser.tabs.onUpdated.addListener(start_all);

mainfest.json:

{
"manifest_version": 2,
"name": "Is this a Google Search?",
"version": "1.0",

"description": "This Addon tells you if you are on Google Search Page",
"icons": {
    "48": "icon/icon.png"
},

"browser_action": {
    "default_icon": "icon/icon.png",
    "default_title": "Is this a Google search?"
},

"permissions": [
    "activeTab"
],

"background": {
    "scripts": ["main.js"]
}
}

There aren't any other background/content scripts.

But if I tried it, there was no message, which said that "Includes google" and the returned value was "false" even if I loaded the Google Page (Instant Search disabled) and searched for something. May you can help me.

Thanks

  • 1
    I suggest you read the [Anatomy of a WebExtension](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension) page (perhaps work through reading the pages linked from there). It has overall architecture information which should help your understanding of how things are generally organized/done. – Makyen Jun 18 '17 at 16:46
  • You should also read the documentaiton [here](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/onUpdated#Parameters) and [here](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/Tab). Basically, `var loc = window.location.href;` doesn't give you the information about the updated tab, only the "location" of the background page, which is useless to you. – Makyen Jun 18 '17 at 23:34
  • Thanks to all of you. I understood the documentation wrong. I thought I could do the same like in the content scripts, in the background scripts, too (Without manipulating the current showed page). I'm now using the message function of "browser.runtime" to send the current page from the content script to the background script. – MyGreatUsername Jun 19 '17 at 13:03
  • I'm glad you found a solution to your problem. Please post an Answer to the question so other people who find this Question can also find a solution. Note that an actual answer/solution should **not** be edited into your Question. You should create your own Answer with the code you used to solve your problem, then accept it (the system may require a 48 hour delay prior to accepting your own answer). When you have solved the problem yourself, [answering your own question is encouraged](http://stackoverflow.com/help/self-answer). – Makyen Jun 19 '17 at 15:35
  • You don't need a content script for that; you can retrive tab URL from background page using `tabs.getAll` or `tabs.query`. For example, see [this answer](https://stackoverflow.com/a/17826527/2468469). – Adrian17 Jun 23 '17 at 14:44

2 Answers2

1

If you use document.URL instead of location.href it should work

if(window.document.URL == "http://example.org"){
 // code
}

The web you want to compare (http://example.org) always includes the protocol.

You can see the reference in the Mozilla Docs: https://developer.mozilla.org/es/docs/Web/API/Document/URL or in W3Schools: https://www.w3schools.com/jsref/prop_doc_url.asp

David
  • 73
  • 5
0

I used a content script which is activated at the domains specified in the manifest.json. It sends a dummy message to the background script via browser.runtime. Then it sends a message to the user