1

拡張ボタンをクリックすると、Web ページの上部に表示されるメニューがあります。ページのリロード時にメニューを表示したままにしておきたいのですが、ドメインをナビゲートするときは可能ですか?

mainifest.json

"background": {
"scripts": ["background.js"],
"persistent": true
 },
  "content_scripts": [
   {
    "matches": ["http://*/*"],
  "css": ["grid.css"],
  "js": ["jquery-2.0.0.min.js"],
       "all_frames": true
   }
 ],

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
   chrome.tabs.executeScript(null, { file: "grid.js" });
});

grid.js

$(document).ready(function() {
$("body").append("<ul id='menu'><li><a id='add' href='#'>Add div</a></li></ul>")
});
4

3 に答える 3

0

マニフェスト.json:

{
    "name": "permenu",
    "version": "1.0",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": {
            "19": "19icon.png"
        },
        "default_title": "permenu"
    },
    "background": {
        "scripts": ["extension.js", "jquery-2.0.0.js"],
        "persistent": true
    },
    "permissions": [
        "tabs"
    ],
    "content_scripts": [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": ["browser.js", "jquery-2.0.0.js"],
            "all_frames": true
        }
    ]
}

拡張子.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendMessage(tab.id, {command: "add" }, function(response) {
            console.log(response.toString());
        });
    });
});

browser.js:

chrome.runtime.onMessage.addListener(function(msg) {
    if (msg.command == "add") {
        doAdd("click");
    }
});

document.onreadystatechange = function() {
    if(document.readyState == "complete") {
        doAdd("onreadystatechange");
    }
};

function doAdd(reason) {
    if(reason == "click") {
        if(document.getElementById("add") == null) {
            localStorage[location.href] = "ready";
            $("body")
              .append("<ul id='menu'><li><a id='add' href='#'>Add div</a></li></ul>");
        }
    } else if(reason == "onreadystatechange") {
        if(localStorage[location.href] == "ready") {
            if(document.getElementById("add") == null) {
                $("body")
                  .append(
                     "<ul id='menu'><li><a id='add' href='#'>Add div</a></li></ul>");
            }
        }
    }
}
于 2013-07-05T10:00:34.790 に答える