如何识别用户是否在特定网站上?

时间:2017-06-18 15:51:43

标签: javascript firefox-webextensions

我尝试创建一个WebExtension,如果它位于特定文件夹中的特定站点上,它会执行某些操作。我试图像这样识别网站和子文件夹:

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"]
}
}

没有任何其他背景/内容脚本。

但是如果我尝试了,就没有消息,即使我加载了Google Page(已禁用即时搜索)并搜索了某些内容,其中包含“包含Google”并且返回的值为“false”。愿你能帮助我。

由于

2 个答案:

答案 0 :(得分:1)

如果您使用document.URL代替location.href,则应该

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

您要比较的网络(http://example.org始终包含协议

您可以在Mozilla文档中看到该引用:https://developer.mozilla.org/es/docs/Web/API/Document/URL或W3Schools:https://www.w3schools.com/jsref/prop_doc_url.asp

答案 1 :(得分:0)

我使用了一个内容脚本,该脚本在manifest.json中指定的域中激活。它通过browser.runtime向后台脚本发送虚拟消息。然后它向用户发送消息