2

Google Chrome 拡張機能を作成しようとしています。コードを実際に挿入していますが、アラート ダイアログ ボックスしか表示されません。

manifest.json:

{
  "name":"Xpress Test Extension",

  "manifest_version": 2,

  "version":"0.2",
  "icons": {
    "16": "icon_16.png",      
    "20": "icon_20.png",
    "128": "icon_128.png"
  },

  "background": { 
    "scripts":  ["background.js","content_script.js"]
  },

  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": "icon_20.png"
  },
  "content_scripts": [
    { 
      "matches": ["https://stackoverflow.com/*"],
      "js" : [ "content_script.js","script.js" ]
    }
  ],

  "content_security_policy": "default-src 'self'",
  "permissions": [
    "tabs",

    "https://stackoverflow.com/*",
    "contextMenus",     "unlimitedStorage"
  ],

  "web_accessible_resources": [
    "icon_20.png",
    "icon_128.png",
    "background.html",
    "content_script.js",
    "popup.html",
    "popup.js",
    "script.js",
    "background.js"
  ]
}

content_script.js(もちろん盗まれました...):

alert('content script!');
var port = chrome.extension.connect();
var rootnode=document.getElementById('topbar');
if (null==rootnode) {
  alert ('could not find div#id topbar');
} else {
  rootnode.addEventListener("click", function() {
    var s = document.createElement('script');
    s.src = chrome.extension.getURL("script.js");
    s.onload = function() {
      this.parentNode.removeChild(this);
    };
    (document.head||document.documentElement).appendChild(s);

    alert( "Hello");
  }, false);
}

script.js (サンプルコードから厚かましくコピー):

 // JS script injection, so that we can read the JS class 'InternalJSVariable'
 var postFriendMap = function() {
 var textarea = document.getElementById('transfer-dom-area');
 textarea.value = JSON.stringify(InternalJSVariable);
 };

 // Create a dummy textarea DOM.
 var textarea = document.createElement('textarea');
 textarea.setAttribute('id', 'transfer-dom-area');
 textarea.style.display = 'none';
 document.body.appendChild(textarea);

 // Start injecting the JS script.
 var script = document.createElement('script');
 script.appendChild(document.createTextNode('(' + postFriendMap + ')();'));
 document.body.appendChild(script);
 alert('injection');

 var textNode=document.createTextNode("CLICK ME");
 var buttonElem=  document.createElement( "button");
 buttonElem.setAttribute("id","extraButton001");
 buttonElem.appendChild(textNode);
 document.getElementById('logo').appendChild(buttonElem);

私のbackground.js(サンプルのさらなる盗難)から:

function genericOnClick(info, tab) {
  console.log("item " + info.menuItemId + " was clicked");
  console.log("info: " + JSON.stringify(info));
  console.log("tab: " + JSON.stringify(tab));
  chrome.tabs.executeScript(null, {file: "content_script.js"});
  console.log('sent to content script...');
}
// Create one test item for each context type.
var contexts = ["page","selection","link","editable","image","video",
             "audio"];
for (var i = 0; i < contexts.length; i++) {
   var context = contexts[i];
   var title = "Test '" + context + "' menu item";
   var id = chrome.contextMenus.create({"title": title, "contexts":[context],
                                    "onclick": genericOnClick});
   console.log("'" + context + "' item:" + id);    
}

// Create a parent item and two children.
var parent = chrome.contextMenus.create({"title": "Test parent item"});
var child1 = chrome.contextMenus.create(
    {"title": "Child 1", "parentId": parent, "onclick": genericOnClick});
var child2 = chrome.contextMenus.create(
    {"title": "Child 2", "parentId": parent, "onclick": genericOnClick});
console.log("parent:" + parent + " child1:" + child1 + " child2:" + child2);
4

0 に答える 0