问题
is it possible to run a PHP script from a Chrome browser bookmark? I've got a PHP script that makes an API call and logs me in to a particular website- instead of using a password program to remember my username and password I want to use the PHP script so I can simply click on it and not have to enter any text.
Is this possible?
回答1:
Create an html document that runs the script, save it to somewhere on your disk, then open it and bookmark it. Every time you click the bookmark the page will open and the script will be run like any other web page.
Alternatively you can look into writing an Extension for yourself. If you know html and php and even a bit of javascript it will be enough to do some damage.
You should be able to make an extension that saves information using sync
and logs you in without opening a new tab in about ten minutes. The only hurdle if you already know html, php, and/or javascript will be the json manifest, but if you know that then you can certainly do some damage.
If you are interested in writing a small extension, this is where i started:
https://developer.chrome.com/extensions/getstarted.html
You can even have the extension open the page you just logged into, then have the extension hide its self with the click of a button or the stroke of a key. Extensions are for the most part pure html and javascript.
One thing to note is that the scripts must be COMPLETELY separate from the html, otherwise Chrome will cry and not run your application. It's a fairly simple workaround for javascript, essentially what i mean is, say you wanted to attach a click
listener on a button
<button id="foo" onclick="bar()"></button>
Is wrong, but you can get around this by using
var button = document.getViewById("foo");
button.addEventListener("click", function() {});
in javascript. In this way the script is separate from the html, and Chrome won't complain at you.
回答2:
PHP is a server-side language, so your PHP script is never running inside the browser. What you will need is a webserver somewhere (presumably on your local PC / network, since otherwise anyone could access it!) which hosts the PHP script at a particular URL. Then you can just bookmark that URL, click the bookmark, and the PHP will run. The script could then redirect to the site itself with a 302 redirect, so it would act like you just had the site bookmarked normally.
Alternatively, rewrite the script in JS and make it into a "bookmarklet" (a bookmark whose target is just a string of javascript to execute when you click, beginning javascript:
) or a Chrome extension, depending how complex you want/need it to be.
来源:https://stackoverflow.com/questions/17408094/how-to-run-a-php-script-from-a-chrome-bookmark